Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 99babba

Browse files
committed
Added abillity to get the value of static properties
1 parent 44509c5 commit 99babba

File tree

4 files changed

+61
-6
lines changed

4 files changed

+61
-6
lines changed

tests/test_classes/example_child_class.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
class example_child_class extends example_class {
66

7+
private static $example_child_static_property = 'very private, such static, wow';
8+
79
private $example_child_string_property;
810

911
public function __construct() {
@@ -23,6 +25,18 @@ public function get_value($property_name) {
2325
}
2426
}
2527

28+
/**
29+
* @inheritDoc
30+
*/
31+
public static function get_static_value(string $property_name) {
32+
// Note the variable variable here
33+
if (!isset(static::$$property_name)) {
34+
return parent::get_static_value($property_name);
35+
} else {
36+
return static::$$property_name;
37+
}
38+
}
39+
2640
private function example_child_method(int $number) {
2741
return ($number * 20);
2842
}

tests/test_classes/example_class.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
class example_class {
66

7+
private static $example_static_property = 'very private, such static, wow';
8+
79
private $example_string_property;
810
private $example_array_property;
911

@@ -24,6 +26,18 @@ public function get_value($property_name) {
2426
return $this->{$property_name};
2527
}
2628

29+
/**
30+
* Similar to get_value() except this will access static properties
31+
*
32+
* @param string $property_name The name of the property
33+
*
34+
* @return mixed The property value
35+
*/
36+
public static function get_static_value(string $property_name) {
37+
// Note the variable variable here
38+
return static::$$property_name;
39+
}
40+
2741
private function example_method(int $number) {
2842
return ($number * 10);
2943
}

tests/utils/ReflectionTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,24 @@ public function dataGetPropertyFromChild(): array {
132132
);
133133
}
134134

135+
public function testGetStaticProperty() {
136+
$property_name = 'example_static_property';
137+
$reflected_value = reflection::get_property(example_class::class, $property_name);
138+
$actual_value = example_class::get_static_value($property_name);
139+
140+
$this->assertInternalType('string', $reflected_value);
141+
$this->assertSame($actual_value, $reflected_value);
142+
}
143+
144+
public function testGetChildStaticProperty() {
145+
$property_name = 'example_child_static_property';
146+
$reflected_value = reflection::get_property(example_child_class::class, $property_name);
147+
$actual_value = example_child_class::get_static_value($property_name);
148+
149+
$this->assertInternalType('string', $reflected_value);
150+
$this->assertSame($actual_value, $reflected_value);
151+
}
152+
135153
public function testSetProperty() {
136154
$expected_value = 'wow such value';
137155
$property_name = 'example_string_property';

utils/reflection.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,43 @@ private static function resolve_method_class(string $class_name, string $method_
6060
/**
6161
* Gets the value of an object property
6262
*
63-
* @param object $object The object to get the value from
63+
* @param mixed $object The object to get the value from or the class name for static properties
6464
* @param string $property_name The name of the property
6565
*
6666
* @return mixed The value
6767
*/
6868
public static function get_property($object, string $property_name) {
69-
$class_name = static::resolve_property_class(get_class($object), $property_name);
69+
$class_name = (is_object($object)) ? get_class($object) : $object;
70+
$class_name = static::resolve_property_class($class_name, $property_name);
7071
$property = new \ReflectionProperty($class_name, $property_name);
7172
$property->setAccessible(true);
7273

73-
return $property->getValue($object);
74+
if (is_object($object)) {
75+
return $property->getValue($object);
76+
} else {
77+
$class = $property->getDeclaringClass();
78+
$values = $class->getStaticProperties();
79+
80+
return $values[$property_name];
81+
}
7482
}
7583

7684
/**
7785
* Sets the value of an object property
7886
*
79-
* @param object $object The object to update
87+
* @param mixed $object The object to update or the name of the class for static properties
8088
* @param string $property_name The name of the property
8189
* @param mixed $value The new value to set
8290
*
8391
* @return void
8492
*/
8593
public static function set_property($object, string $property_name, $value) {
86-
$class_name = static::resolve_property_class(get_class($object), $property_name);
94+
$class_name = (is_object($object)) ? get_class($object) : $object;
95+
$class_name = static::resolve_property_class($class_name, $property_name);
8796
$property = new \ReflectionProperty($class_name, $property_name);
8897
$property->setAccessible(true);
8998

90-
$property->setValue($object, $value);
99+
$property->setValue((is_object($object)) ? $object : null, $value);
91100
}
92101

93102
/**

0 commit comments

Comments
 (0)