Skip to content

Add ReflectionProperty::isDynamic() as an alternative to isDefault() #15758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UPGRADING
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,7 @@ PHP 8.4 UPGRADE NOTES
- ReflectionClass::SKIP_INITIALIZATION_ON_SERIALIZE
- ReflectionClass::SKIP_DESTRUCTOR
RFC: https://wiki.php.net/rfc/lazy-objects
. ReflectionProperty::isDynamic() was introduced.

- SOAP:
. Added support for clark notation for namespaces in class map.
Expand Down
19 changes: 16 additions & 3 deletions ext/reflection/php_reflection.c
Original file line number Diff line number Diff line change
Expand Up @@ -5921,8 +5921,7 @@ ZEND_METHOD(ReflectionProperty, isVirtual)
_property_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_VIRTUAL);
}

/* {{{ Returns whether this property is default (declared at compilation time). */
ZEND_METHOD(ReflectionProperty, isDefault)
static void _property_check_dynamic(INTERNAL_FUNCTION_PARAMETERS, bool dynamic_true)
{
reflection_object *intern;
property_reference *ref;
Expand All @@ -5931,7 +5930,21 @@ ZEND_METHOD(ReflectionProperty, isDefault)
RETURN_THROWS();
}
GET_REFLECTION_OBJECT_PTR(ref);
RETURN_BOOL(ref->prop != NULL);
bool is_dynamic = ref->prop == NULL;
RETURN_BOOL(dynamic_true ? is_dynamic : !is_dynamic);
}

/* {{{ Returns whether this property is default (declared at compilation time). */
ZEND_METHOD(ReflectionProperty, isDefault)
{
_property_check_dynamic(INTERNAL_FUNCTION_PARAM_PASSTHRU, false);
}
/* }}} */

/* {{{ Returns whether this property is dynamic (not declared at compilation time). */
ZEND_METHOD(ReflectionProperty, isDynamic)
{
_property_check_dynamic(INTERNAL_FUNCTION_PARAM_PASSTHRU, true);
}
/* }}} */

Expand Down
2 changes: 2 additions & 0 deletions ext/reflection/php_reflection.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,8 @@ public function isReadOnly(): bool {}
/** @tentative-return-type */
public function isDefault(): bool {}

public function isDynamic(): bool {}

public function isAbstract(): bool {}

public function isVirtual(): bool {}
Expand Down
6 changes: 5 additions & 1 deletion ext/reflection/php_reflection_arginfo.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 38 additions & 4 deletions ext/reflection/tests/ReflectionProperty_basic2.phpt
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
--TEST--
Test usage of ReflectionProperty methods isDefault(), getModifiers(), getDeclaringClass() and getDocComment().
Test usage of ReflectionProperty methods isDefault(), isDynamic(), getModifiers(), getDeclaringClass() and getDocComment().
--INI--
opcache.save_comments=1
--FILE--
<?php

function reflectProperty($class, $property) {
$propInfo = new ReflectionProperty($class, $property);
function reflectProperty($classOrObj, $property, $className = null) {
$className ??= $classOrObj;
$propInfo = new ReflectionProperty($classOrObj, $property);
echo "**********************************\n";
echo "Reflecting on property $class::$property\n\n";
echo "Reflecting on property $className::$property\n\n";
echo "isDefault():\n";
var_dump($propInfo->isDefault());
echo "isDynamic():\n";
var_dump($propInfo->isDynamic());
echo "getModifiers():\n";
var_dump($propInfo->getModifiers());
echo "getDeclaringClass():\n";
Expand All @@ -20,6 +23,7 @@ function reflectProperty($class, $property) {
echo "\n**********************************\n";
}

#[AllowDynamicProperties]
class TestClass {
public $pub;
static public $stat = "static property";
Expand All @@ -35,13 +39,19 @@ reflectProperty("TestClass", "stat");
reflectProperty("TestClass", "prot");
reflectProperty("TestClass", "priv");

$obj = new TestClass();
$obj->dyn = 'dynamic';
reflectProperty($obj, "dyn", "TestClass");

?>
--EXPECTF--
**********************************
Reflecting on property TestClass::pub

isDefault():
bool(true)
isDynamic():
bool(false)
getModifiers():
int(1)
getDeclaringClass():
Expand All @@ -58,6 +68,8 @@ Reflecting on property TestClass::stat

isDefault():
bool(true)
isDynamic():
bool(false)
getModifiers():
int(17)
getDeclaringClass():
Expand All @@ -74,6 +86,8 @@ Reflecting on property TestClass::prot

isDefault():
bool(true)
isDynamic():
bool(false)
getModifiers():
int(2)
getDeclaringClass():
Expand All @@ -92,6 +106,8 @@ Reflecting on property TestClass::priv

isDefault():
bool(true)
isDynamic():
bool(false)
getModifiers():
int(4)
getDeclaringClass():
Expand All @@ -103,3 +119,21 @@ getDocComment():
bool(false)

**********************************
**********************************
Reflecting on property TestClass::dyn

isDefault():
bool(false)
isDynamic():
bool(true)
getModifiers():
int(1)
getDeclaringClass():
object(ReflectionClass)#%d (1) {
["name"]=>
string(9) "TestClass"
}
getDocComment():
bool(false)

**********************************
69 changes: 69 additions & 0 deletions ext/reflection/tests/ReflectionProperty_isDynamic_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
--TEST--
Test ReflectionProperty::isDynamic() usage.
--FILE--
<?php

function reflectProperty($classOrObj, $property, $className = null) {
$className ??= $classOrObj;
$propInfo = new ReflectionProperty($classOrObj, $property);
echo "**********************************\n";
echo "Reflecting on property $className::$property\n\n";
echo "isDynamic():\n";
var_dump($propInfo->isDynamic());
echo "\n**********************************\n";
}

#[AllowDynamicProperties]
class TestClass {
public $pub;
static public $stat = "static property";
protected $prot = 4;
private $priv = "keepOut";
}

reflectProperty("TestClass", "pub");
reflectProperty("TestClass", "stat");
reflectProperty("TestClass", "prot");
reflectProperty("TestClass", "priv");

$obj = new TestClass();
$obj->dyn = 'dynamic';
reflectProperty($obj, "dyn", "TestClass");

?>
--EXPECT--
**********************************
Reflecting on property TestClass::pub

isDynamic():
bool(false)

**********************************
**********************************
Reflecting on property TestClass::stat

isDynamic():
bool(false)

**********************************
**********************************
Reflecting on property TestClass::prot

isDynamic():
bool(false)

**********************************
**********************************
Reflecting on property TestClass::priv

isDynamic():
bool(false)

**********************************
**********************************
Reflecting on property TestClass::dyn

isDynamic():
bool(true)

**********************************
Loading