Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'pull-request/96' into PHP-5.4
* pull-request/96: Implemented FR #61602 Allow access to name of constant used as default value
- Loading branch information
Showing
5 changed files
with
214 additions
and
12 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
52 changes: 52 additions & 0 deletions
52
ext/reflection/tests/ReflectionParameter_DefaultValueConstant_basic1.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,52 @@ | ||
--TEST-- | ||
ReflectionParameter::isDefaultValueConstant() && getDefaultValueConstantName() | ||
--FILE-- | ||
<?php | ||
|
||
define("CONST_TEST_1", "const1"); | ||
|
||
function ReflectionParameterTest($test1=array(), $test2 = CONST_TEST_1) { | ||
echo $test; | ||
} | ||
$reflect = new ReflectionFunction('ReflectionParameterTest'); | ||
foreach($reflect->getParameters() as $param) { | ||
if($param->getName() == 'test1') { | ||
var_dump($param->isDefaultValueConstant()); | ||
} | ||
if($param->getName() == 'test2') { | ||
var_dump($param->isDefaultValueConstant()); | ||
} | ||
if($param->isDefaultValueAvailable() && $param->isDefaultValueConstant()) { | ||
var_dump($param->getDefaultValueConstantName()); | ||
} | ||
} | ||
|
||
class Foo2 { | ||
const bar = 'Foo2::bar'; | ||
} | ||
|
||
class Foo { | ||
const bar = 'Foo::bar'; | ||
|
||
public function baz($param1 = self::bar, $param2=Foo2::bar, $param3=CONST_TEST_1) { | ||
} | ||
} | ||
|
||
$method = new ReflectionMethod('Foo', 'baz'); | ||
$params = $method->getParameters(); | ||
|
||
foreach ($params as $param) { | ||
if ($param->isDefaultValueConstant()) { | ||
var_dump($param->getDefaultValueConstantName()); | ||
} | ||
} | ||
?> | ||
==DONE== | ||
--EXPECT-- | ||
bool(false) | ||
bool(true) | ||
string(12) "CONST_TEST_1" | ||
string(9) "self::bar" | ||
string(9) "Foo2::bar" | ||
string(12) "CONST_TEST_1" | ||
==DONE== |
30 changes: 30 additions & 0 deletions
30
ext/reflection/tests/ReflectionParameter_DefaultValueConstant_basic2.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,30 @@ | ||
--TEST-- | ||
ReflectionParameter::isDefaultValueConstant() && getDefaultValueConstantName() for namespace | ||
--FILE-- | ||
<?php | ||
|
||
namespace ReflectionTestNamespace { | ||
CONST TEST_CONST_1 = "Test Const 1"; | ||
|
||
class TestClass { | ||
const TEST_CONST_2 = "Test Const 2 in class"; | ||
} | ||
} | ||
|
||
namespace { | ||
function ReflectionParameterTest($test=ReflectionTestNamespace\TestClass::TEST_CONST_2, $test2 = ReflectionTestNamespace\CONST_TEST_1) { | ||
echo $test; | ||
} | ||
$reflect = new ReflectionFunction('ReflectionParameterTest'); | ||
foreach($reflect->getParameters() as $param) { | ||
if($param->isDefaultValueAvailable() && $param->isDefaultValueConstant()) { | ||
echo $param->getDefaultValueConstantName() . "\n"; | ||
} | ||
} | ||
echo "==DONE=="; | ||
} | ||
?> | ||
--EXPECT-- | ||
ReflectionTestNamespace\TestClass::TEST_CONST_2 | ||
ReflectionTestNamespace\CONST_TEST_1 | ||
==DONE== |
25 changes: 25 additions & 0 deletions
25
ext/reflection/tests/ReflectionParameter_DefaultValueConstant_error.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,25 @@ | ||
--TEST-- | ||
ReflectionParameter::getDefaultValueConstant() should raise exception on non optional parameter | ||
--FILE-- | ||
<?php | ||
|
||
define("CONST_TEST_1", "const1"); | ||
|
||
function ReflectionParameterTest($test, $test2 = CONST_TEST_1) { | ||
echo $test; | ||
} | ||
$reflect = new ReflectionFunction('ReflectionParameterTest'); | ||
foreach($reflect->getParameters() as $param) { | ||
try { | ||
echo $param->getDefaultValueConstantName() . "\n"; | ||
} | ||
catch(ReflectionException $e) { | ||
echo $e->getMessage() . "\n"; | ||
} | ||
} | ||
?> | ||
==DONE== | ||
--EXPECT-- | ||
Parameter is not optional | ||
CONST_TEST_1 | ||
==DONE== |