1

I'm trying to write some code around a single file that somebody else wrote. That file includes some variables, which are originally intended to be changed directly inside that file, but I wanna keep the file itself intact, so that it can be updated via git/composer without conflict.

The variables from that file are immediately called:

// file.php
$VARIABLE = 'foo'; // change this to your settings 

echo $VARIABLE;

is it possible to somehow overwrite $VARIABLE from outside that file, so that it will be used in the function call, without needing to make any changes to that file itself?

Edit: I mean in a way that either changes it in between the two lines above or by somehow declaring $VARIABLE = 'bar' globally in a way that just silently ignores the redeclaration to foo, instead of throwing an error.

5
  • $VARIABLE = 'foo'; // change this (foo) to your settings Commented Jan 13, 2021 at 10:11
  • the question is if that's possible without editing the file itself. Commented Jan 13, 2021 at 10:12
  • You can use mysql? Commented Jan 13, 2021 at 10:14
  • 4
    "is it possible to somehow overwrite $VARIABLE from outside that file" - Sure, just do: $VARIABLE = 'pancake'; and you've overwritten it. However, if you echo the variable directly after you've set it to foo, there's no way for someone to change it in between those line. Commented Jan 13, 2021 at 10:17
  • thanks, I thought so. Was hoping that it's somehow possible to declare $VARIABLE = 'pancake' globally in a way that just silently ignores the redeclaration to foo, instead of throwing an error. Commented Jan 13, 2021 at 10:21

1 Answer 1

1

I would say it's impossible just by doing it by simple PHP. The only solution which comes to my mind is to open the file (by PHP (fopen)) modify line between setting and reading of variable to something, what will do modifications you like. Then save its copy (don't save to original file) as a temporary file, include newly created file and run it. After that, you can delete temp file (or if u do some kind of smart caching, the file can stay, and can be overwritten in need).

All those instructions are algorithm to write in PHP, not steps you should do to make it work.

It's not the wished solution, but if the setting of variable is immediately before reading from it, there is no way you can modify that variable without modifying original file.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, yeah I wouldn't wanna programmatically modify the file. Guess I just have to manually keep track of changes to the file in question and update it whenever needed.
@ottosleger if you suspect revenge voting then please flag one of your posts for moderator attention. They will look into it. Also, you shouldn't assume that downvotes are because of a reason. They are subjective by design.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.