Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add $filter parameter for ReflectionClass::(getConstants|getReflectio…
…nConstants) This solves [#79628](https://bugs.php.net/79628). Similar to `ReflectionClass::getMethods()` and `ReflectionClass::getProperties()`, this new `$filter` argument allows the filtering of constants defined in a class by their visibility. For that, we create three new constants for `ReflectionClassConstant`: * `IS_PUBLIC` * `IS_PROTECTED` * `IS_PRIVATE` Closes GH-5649.
- Loading branch information
1 parent
84492f5
commit 7439941
Showing
8 changed files
with
188 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
ext/reflection/tests/ReflectionClass_getConstants_filter.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
--TEST-- | ||
ReflectionClass::getConstants() with $filter | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const PUBLIC_CONST = 'BAR'; | ||
public const ANOTHER_PUBLIC_CONST = 'BAZ'; | ||
protected const PROTECTED_CONST = 'FOO'; | ||
private const PRIVATE_CONST = 'QUOZ'; | ||
} | ||
|
||
class B { | ||
public const PUBLIC_CONST = 'BAR'; | ||
protected const ANOTHER_PROTECTED_CONST = 'BAZ'; | ||
protected const PROTECTED_CONST = 'FOO'; | ||
private const PRIVATE_CONST = 'QUOZ'; | ||
} | ||
|
||
class C { | ||
public const PUBLIC_CONST = 'BAR'; | ||
protected const PROTECTED_CONST = 'FOO'; | ||
private const PRIVATE_CONST = 'QUOZ'; | ||
private const ANOTHER_PRIVATE_CONST = 'BAZ'; | ||
} | ||
|
||
$reflectionClassA = new ReflectionClass(A::class); | ||
var_dump($reflectionClassA->getConstants(ReflectionClassConstant::IS_PUBLIC)); | ||
|
||
$reflectionClassB = new ReflectionClass(B::class); | ||
var_dump($reflectionClassB->getConstants(ReflectionClassConstant::IS_PROTECTED)); | ||
|
||
$reflectionClassC = new ReflectionClass(C::class); | ||
var_dump($reflectionClassC->getConstants(ReflectionClassConstant::IS_PRIVATE)); | ||
?> | ||
--EXPECTF-- | ||
array(%d) { | ||
["PUBLIC_CONST"]=> | ||
string(%d) "BAR" | ||
["ANOTHER_PUBLIC_CONST"]=> | ||
string(%d) "BAZ" | ||
} | ||
array(%d) { | ||
["ANOTHER_PROTECTED_CONST"]=> | ||
string(%d) "BAZ" | ||
["PROTECTED_CONST"]=> | ||
string(%d) "FOO" | ||
} | ||
array(%d) { | ||
["PRIVATE_CONST"]=> | ||
string(%d) "QUOZ" | ||
["ANOTHER_PRIVATE_CONST"]=> | ||
string(%d) "BAZ" | ||
} |
83 changes: 83 additions & 0 deletions
83
ext/reflection/tests/ReflectionClass_getReflectionConstants_filter.phpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
--TEST-- | ||
ReflectionClass::getReflectionConstants() with $filter | ||
--FILE-- | ||
<?php | ||
class A { | ||
public const PUBLIC_CONST = 'BAR'; | ||
public const ANOTHER_PUBLIC_CONST = 'BAZ'; | ||
protected const PROTECTED_CONST = 'FOO'; | ||
private const PRIVATE_CONST = 'QUOZ'; | ||
} | ||
|
||
class B { | ||
public const PUBLIC_CONST = 'BAR'; | ||
protected const ANOTHER_PROTECTED_CONST = 'BAZ'; | ||
protected const PROTECTED_CONST = 'FOO'; | ||
private const PRIVATE_CONST = 'QUOZ'; | ||
} | ||
|
||
class C { | ||
public const PUBLIC_CONST = 'BAR'; | ||
protected const PROTECTED_CONST = 'FOO'; | ||
private const PRIVATE_CONST = 'QUOZ'; | ||
private const ANOTHER_PRIVATE_CONST = 'BAZ'; | ||
} | ||
|
||
$reflectionClassA = new ReflectionClass(A::class); | ||
var_dump($reflectionClassA->getReflectionConstants(ReflectionClassConstant::IS_PUBLIC)); | ||
|
||
$reflectionClassB = new ReflectionClass(B::class); | ||
var_dump($reflectionClassB->getReflectionConstants(ReflectionClassConstant::IS_PROTECTED)); | ||
|
||
$reflectionClassC = new ReflectionClass(C::class); | ||
var_dump($reflectionClassC->getReflectionConstants(ReflectionClassConstant::IS_PRIVATE)); | ||
?> | ||
--EXPECTF-- | ||
array(2) { | ||
[0]=> | ||
object(ReflectionClassConstant)#%d (%d) { | ||
["name"]=> | ||
string(%d) "PUBLIC_CONST" | ||
["class"]=> | ||
string(%d) "A" | ||
} | ||
[1]=> | ||
object(ReflectionClassConstant)#%d (%d) { | ||
["name"]=> | ||
string(%d) "ANOTHER_PUBLIC_CONST" | ||
["class"]=> | ||
string(%d) "A" | ||
} | ||
} | ||
array(2) { | ||
[0]=> | ||
object(ReflectionClassConstant)#%d (%d) { | ||
["name"]=> | ||
string(%d) "ANOTHER_PROTECTED_CONST" | ||
["class"]=> | ||
string(%d) "B" | ||
} | ||
[1]=> | ||
object(ReflectionClassConstant)#%d (%d) { | ||
["name"]=> | ||
string(%d) "PROTECTED_CONST" | ||
["class"]=> | ||
string(%d) "B" | ||
} | ||
} | ||
array(2) { | ||
[0]=> | ||
object(ReflectionClassConstant)#%d (%d) { | ||
["name"]=> | ||
string(%d) "PRIVATE_CONST" | ||
["class"]=> | ||
string(%d) "C" | ||
} | ||
[1]=> | ||
object(ReflectionClassConstant)#%d (%d) { | ||
["name"]=> | ||
string(%d) "ANOTHER_PRIVATE_CONST" | ||
["class"]=> | ||
string(%d) "C" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters