mirror of
https://github.com/wiosna-dev/common-library.git
synced 2026-03-12 17:41:50 +01:00
Reflection > getPropertyValue() method > look for the property in parent classes
This commit is contained in:
@@ -7,6 +7,7 @@ Common and useful classes, methods, exceptions etc.
|
||||
1. Travis CI > run many tasks using Phing > update configuration
|
||||
2. Template with placeholders > verification of placeholders without values > make stronger and point out which are
|
||||
missing
|
||||
3. Reflection > getPropertyValue() method > look for the property in parent classes
|
||||
|
||||
# 1.0.2
|
||||
|
||||
|
||||
@@ -217,6 +217,27 @@ class Reflection
|
||||
} catch (\ReflectionException $exception) {
|
||||
/*
|
||||
* 2nd try:
|
||||
* Look for the property in parent classes
|
||||
*/
|
||||
if (null === $reflectionProperty) {
|
||||
$propertyFound = false;
|
||||
$reflectionProperties = self::getProperties($object, null, true);
|
||||
|
||||
foreach ($reflectionProperties as $reflectionProperty) {
|
||||
if ($reflectionProperty->getName() === $property) {
|
||||
$propertyFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if ($propertyFound && null !== $reflectionProperty) {
|
||||
$reflectionProperty->setAccessible(true);
|
||||
$value = $reflectionProperty->getValue($object);
|
||||
$reflectionProperty->setAccessible(false);
|
||||
}
|
||||
} else {
|
||||
/*
|
||||
* 3rd try:
|
||||
* Look for the get / has / is methods
|
||||
*/
|
||||
$class = new \ReflectionObject($object);
|
||||
@@ -255,11 +276,11 @@ class Reflection
|
||||
}
|
||||
}
|
||||
|
||||
if (!$valueFound && null !== $reflectionProperty) {
|
||||
if (!$valueFound) {
|
||||
/*
|
||||
* Oops, value of the property is still unknown
|
||||
*
|
||||
* 3rd try:
|
||||
* 4th try:
|
||||
* Let's modify accessibility of the property and try again to get value
|
||||
*/
|
||||
$reflectionProperty->setAccessible(true);
|
||||
@@ -268,6 +289,7 @@ class Reflection
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
@@ -309,6 +309,12 @@ class ReflectionTest extends BaseTestCase
|
||||
self::assertEquals($username, Reflection::getPropertyValue($f, 'username'));
|
||||
}
|
||||
|
||||
public function testGetPropertyValueFromParentClass(): void
|
||||
{
|
||||
$c = new C();
|
||||
self::assertEquals(1, Reflection::getPropertyValue($c, 'count', true));
|
||||
}
|
||||
|
||||
public function testGetPropertyValuesFromEmptySource()
|
||||
{
|
||||
self::assertEquals([], Reflection::getPropertyValues([], 'something'));
|
||||
|
||||
Reference in New Issue
Block a user