Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to know if static property is inherited in php?

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?

like image 286
Matrix Avatar asked Jul 11 '15 15:07

Matrix


People also ask

Can static methods be inherited PHP?

PHP Static Methods with Inheritance ExampleStatic methods can be accessed from the methods in the same class using the self keyword and :: .

How can I get static properties in PHP?

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 .

How use $this in static method in PHP?

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.

Can private variables be inherited in PHP?

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.


2 Answers

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

like image 53
Adrian Cid Almaguer Avatar answered Oct 19 '22 23:10

Adrian Cid Almaguer


Edit: This answer is valid only for revision #1 of the question, when the parent class contains no property by the same name.

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
like image 27
Siguza Avatar answered Oct 20 '22 01:10

Siguza