I have a $class_name = 'B';
And :
class A
{
static $foo = 42;
static $baz = 4;
}
class B extends A
{
static $bar = 2;
static $baz = 44;
}
How can I know if $class_name::$foo
is a static property for the $class_name or if it's a inherited static property?
I need following result :
$class_name = 'A';
isOwnStaticProperty($class_name, 'foo'); //TRUE : is a static property of this class
$class_name = 'B';
isOwnStaticProperty($class_name, 'foo'); //FALSE : is NOT a static property of this class
$class_name = 'B';
isOwnStaticProperty($class_name, 'bar'); //TRUE : is a static property of this class
$class_name = 'A';
isOwnStaticProperty($class_name, 'bar'); //FALSE : is NOT a static property of this class
$class_name = 'B';
isOwnStaticProperty($class_name, 'baz'); //TRUE : is a static property of this class
$class_name = 'A';
isOwnStaticProperty($class_name, 'baz'); //TRUE : is a static property of this class
How implement isOwnStaticProperty()
function?
PHP Static Methods with Inheritance ExampleStatic methods can be accessed from the methods in the same class using the self keyword and :: .
Static properties are accessed using the Scope Resolution Operator ( :: ) and cannot be accessed through the object operator ( -> ). It's possible to reference the class using a variable. The variable's value cannot be a keyword (e.g. self , parent and static ). print $foo::$my_static .
The self keyword is used to represent the current and static members of the class. The $this variable is used to represent the current object and non-static members of the class. The features and the uses of self keywords and $this variable in PHP class are explained in this tutorial.
You see, if they try to alter a private variable directly PHP will automatically spit out an error message. However, if that private variable was inherited from another class, PHP will try to accommodate their request by having a private variable and a public variable.
You can use the ReflectionClass with the method getProperties to retrieves the reflected properties. To filter you can use ReflectionProperty
<?php
class A
{
static $foo = 42;
static $baz = 4;
}
class B extends A
{
static $bar = 2;
static $baz = 44;
}
function isOwnStaticProperty($class, $prop) {
$reflect = new ReflectionClass($class);
//Filtering only the statics values with ReflectionProperty::IS_STATIC
$props = $reflect->getProperties(ReflectionProperty::IS_STATIC);
foreach ($props as $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
Use get_parent_class
in combination with isset
and variable variables:
function isOwnStaticProperty($class, $property)
{
$parent = get_parent_class($class);
return isset($class::$$property) && ($parent === FALSE || !isset($parent::$$property));
}
This checks whether $class
has a static property named $property
, and either has no parent class, or the parent class does not have such a property.
Note the two $
before property
in isOwnStaticProperty
.
Call it like
echo isOwnStaticProperty('A', 'foo'); // TRUE
echo isOwnStaticProperty('A', 'bar'); // FALSE
echo isOwnStaticProperty('B', 'foo'); // FALSE
echo isOwnStaticProperty('B', 'bar'); // TRUE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With