I have a unique situation that is hard to debug.
I need to set a global string inside a php class that is not strictly defined as a global, the class must be in another file.
The file.php with the string has simply this:
//this cannot be changed
$foo_version = '1.1.1';
The example.php file trying to access this string must use a class:
class Bar extends Task {
public function main() {
require_once('../file.php');
//global $foo_version; this doesn't work
// update the database with this string, does not work
update_option( 'db_field', $foo_version );
}
}
How can I get the $foo_version to return something inside the class?
Also nothing can be defined/done outside the class.
What you have is a variable
$foo_versionand it can easily be changed. You can usedefineto make it a constant instead see PHP DOCIf you insist Replace
require_oncewithrequireyour code would work because PHP will check if the file has already been included, and if so, not include (require) it again.If you have used this in another class definitely it would not work. Change your code to the following