Getting static property from a class with dynamic class name in PHP
If you are using PHP 5.3.0 or greater, you can use the following:
$classname::$$propertyname;
Unfortunately, if you are using a version lower than 5.3.0, you are stuck using eval()
(get_class_vars()
will not work if the value is dynamic).
$value = eval($classname.'::$'.$propertyname.';');
EDIT: I've just said get_class_vars()
wouldn't work if the value is dynamic, but apparently, variable static members are part of "the default properties of a class". You could use the following wrapper:
function get_user_prop($className, $property) {
if(!class_exists($className)) return null;
if(!property_exists($className, $property)) return null;
$vars = get_class_vars($className);
return $vars[$property];
}
class Foo { static $bar = 'Fizz'; }
echo get_user_prop('Foo', 'bar'); // echoes Fizz
Foo::$bar = 'Buzz';
echo get_user_prop('Foo', 'bar'); // echoes Buzz
Unfortunately, if you want to set the value of the variable, you will still need to use eval()
, but with some validation in place, it's not so evil.
function set_user_prop($className, $property,$value) {
if(!class_exists($className)) return false;
if(!property_exists($className, $property)) return false;
/* Since I cannot trust the value of $value
* I am putting it in single quotes (I don't
* want its value to be evaled. Now it will
* just be parsed as a variable reference).
*/
eval($className.'::$'.$property.'=$value;');
return true;
}
class Foo { static $bar = 'Fizz'; }
echo get_user_prop('Foo', 'bar'); // echoes Fizz
set_user_prop('Foo', 'bar', 'Buzz');
echo get_user_prop('Foo', 'bar'); // echoes Buzz
set_user_prop()
with this validation should be secure. If people start putting random things as $className
and $property
, it will exit out of the function as it won't be an existing class or property. As of $value
, it is never actually parsed as code so whatever they put in there won't affect the script.
PHP Access static property from dynamic class name
I solved it.
The static variable can be accessed by:
public function getMyVar($classname) {
return $classname::$myvar;
}
That was a lot easier than anticipated! :)
How to access static property and static constants of another Class Dynamically
comment line 11
and 12
and try to var_dump
this->config
and you'll see that it is not picking the whole object but just the __construct
because you are calling static methods on an object so try the following code
class HelloAction
{
const FIRST = "DEMO";
public static $first = "WORLD";
function __construct($confclassname)
{
$this->config = $confclassname;
# PHP Interpreter throws an Error for These Two Lines
$this->first1 = $confclassname::$a;
$this->first2 = $confclassname::$b;
}
function setFirst($s)
{
self::$first = $s;
}
}
class action1
{
const AAAA = "____Hello World____";
public static $a = "this is an apple";
public static $b = "That is an Dog";
function __construct()
{
$this->second = "hi, there.";
}
}
class action2
{
const AAAA = "___Yeah, Hello World____";
public static $a = "Whare You were...";
public static $b = "Awesome work";
function __construct()
{
}
public static function action21($s)
{
self::$a = $s;
}
public function action22()
{
return self::$a;
}
}
$b1 = new HelloAction('action1');
$b2 = new HelloAction('action2');
echo $b1->first1 . "\n";
echo $b1->first2 . "\n";
?>
Setting a static class property dynamically with squiqlies
You can't set dynamically a static property (otherwise wasn't static :P), but you can manage it, example
class Foo {
static $vars;
public static function set( $k, $v ){
self::$vars[$k] = $v;
}
public static function get( $k ){
return isset(self::$vars[$k]) ? self::$vars[$k] : 'error';
}
}
accessing static methods using a variable class name (PHP)
That syntax is only supported in PHP 5.3 and later. Previous versions don't understand that syntax, hence your parse error (T_PAAMAYIM_NEKUDOTAYIM
refers to the ::
operator).
In previous versions you can try call_user_func()
, passing it an array containing the class name and its method name:
$variable_class_name = 'foo';
call_user_func(array($variable_class_name, 'bar'));
PHP how to set static variable from a string
You can use a plain variable variable name:
static function databaseInit( $database ){
self::$$database = true;
}
.. but you should really rework it to just keep an array and manipulate the keys of the array instead, as that will keep all the settings inside a single namespace instead of potentially doing weird stuff with other static variables if a name is mistyped, etc.
class app {
static $databases = [];
...
static function databaseInit($database) {
self::$databases[$database] = true;
}
}
And the next step would be to make the class non-static instead, so it can be easier tested and will keep its state locally instead.
Access Static properties using PHP object
Static properties may be accessed on various ways.
Class::$aStaticProp; //by class name
$classname::$aStaticProp; // As of PHP 5.3.0 by object instance
Static properties cannot be accessed through the object using the arrow operator ->
.
As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static).
More you can read in manual
Access a static variable by $var::$reference
You can use reflection to do this. Create a ReflectionClass object given the classname, and then use the getStaticPropertyValue method to get the static variable value.
class Demo
{
public static $foo = 42;
}
$class = new ReflectionClass('Demo');
$value=$class->getStaticPropertyValue('foo');
var_dump($value);
Related Topics
Set Active State on Navigation Dynamically
PHP Function That Creates a Nested Ul Li
Generating Random Numbers from Skewed Normal Distribution
What Ocr Options Exist Beyond Tesseract
How So I Convert This Sha256 + Base64 from Swift to PHP
Cast the Current Object ($This) to a Descendent Class
How to Count Same Values in an Array and Store It to a Variable
Fatal Error: Call to Undefined Method MySQLi_Result::Fetch_All()
PHP Foreach by Reference Causes Weird Glitch When Going Through Array of Objects
Woocommerce: Display Some Reviews Randomly on Home Page
How to Query Employee Details and Relate Their Performance Metrics
PHP Datetime() Class, Change First Day of the Week to Monday
PHP How to Write Data to Middle of File Without Rewriting File