class - How to know if static property is inherited in php? -


i have $class_name = 'b';

and :

class {     static $foo = 42;     static $baz = 4; }  class b extends {    static $bar = 2;    static $baz = 44; } 

how can know if $class_name::$foo static property $class_name or if it's inherited static property?

i need following result :

$class_name = 'a'; isownstaticproperty($class_name, 'foo'); //true : static property of class  $class_name = 'b'; isownstaticproperty($class_name, 'foo'); //false : not static property of class  $class_name = 'b'; isownstaticproperty($class_name, 'bar'); //true : static property of class  $class_name = 'a'; isownstaticproperty($class_name, 'bar'); //false : not static property of class  $class_name = 'b'; isownstaticproperty($class_name, 'baz'); //true : static property of class  $class_name = 'a'; isownstaticproperty($class_name, 'baz'); //true : static property of class 

how implement isownstaticproperty() function?

you can use reflectionclass method getproperties retrieves reflected properties. filter can use reflectionproperty

<?php  class {     static $foo = 42;     static $baz = 4; }  class b extends {    static $bar = 2;    static $baz = 44; }  function isownstaticproperty($class, $prop) {     $reflect = new reflectionclass($class);     //filtering statics values reflectionproperty::is_static     $props   = $reflect->getproperties(reflectionproperty::is_static);     foreach ($props $object) {         if($object->class == $class && $object->name == $prop) {             return true;         }         }     return false; }      $class_name = 'a'; echo isownstaticproperty($class_name, 'foo') ? "true<br>\n" : "false<br>\n";   $class_name = 'b'; echo isownstaticproperty($class_name, 'foo') ? "true<br>\n" : "false<br>\n";   $class_name = 'b'; echo isownstaticproperty($class_name, 'bar') ? "true<br>\n" : "false<br>\n";   $class_name = 'a'; echo isownstaticproperty($class_name, 'bar') ? "true<br>\n" : "false<br>\n";   $class_name = 'b'; echo isownstaticproperty($class_name, 'baz') ? "true<br>\n" : "false<br>\n";   $class_name = 'a'; echo isownstaticproperty($class_name, 'baz') ? "true<br>\n" : "false<br>\n";  

output:

true
false
true
false
true
true


Comments