parent
edb726efa6
commit
1527a44854
2 changed files with 87 additions and 45 deletions
@ -1,41 +1,81 @@ |
|||||||
--TEST-- |
--TEST-- |
||||||
Class constant referenced via self:: / parent:: / ClassName:: as a property default value |
Class constants referenced through self, parent, explicit names and runtime static |
||||||
--FILE-- |
--FILE-- |
||||||
<?php |
<?php |
||||||
|
|
||||||
declare(strict_types=1); |
declare(strict_types=1); |
||||||
|
|
||||||
class A |
namespace StubConstRef { |
||||||
{ |
class Base |
||||||
const A = 1; |
{ |
||||||
const B = 2; |
const INHERITED = 1; |
||||||
} |
const OVERRIDDEN = 2; |
||||||
|
} |
||||||
|
|
||||||
|
class Child extends Base |
||||||
|
{ |
||||||
|
const LOCAL = 10; |
||||||
|
const OVERRIDDEN = 20; |
||||||
|
const SELF_LOCAL = self::LOCAL; |
||||||
|
const SELF_INHERITED = self::INHERITED; |
||||||
|
const PARENT_VALUE = parent::OVERRIDDEN; |
||||||
|
|
||||||
|
public int $selfLocal = self::LOCAL; |
||||||
|
public int $selfInherited = self::INHERITED; |
||||||
|
public int $parentValue = parent::OVERRIDDEN; |
||||||
|
public int $explicitValue = \StubConstRef\Base::INHERITED; |
||||||
|
|
||||||
|
public function defaults( |
||||||
|
int $self = self::LOCAL, |
||||||
|
int $parent = parent::OVERRIDDEN |
||||||
|
): array { |
||||||
|
return [$self, $parent]; |
||||||
|
} |
||||||
|
|
||||||
|
public function runtimeStatic(): int |
||||||
|
{ |
||||||
|
return static::OVERRIDDEN; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
class B extends A |
class GrandChild extends Child |
||||||
{ |
{ |
||||||
// self:: referencing an inherited constant. |
const OVERRIDDEN = 30; |
||||||
// Bug: self::A was incorrectly resolved as B::B::A and the lookup failed. |
} |
||||||
public $a = self::A; |
|
||||||
// self:: referencing a constant declared in the same class. |
|
||||||
const LOCAL = 10; |
|
||||||
public $local = self::LOCAL; |
|
||||||
// parent:: referencing a parent constant. |
|
||||||
public $b = parent::A; |
|
||||||
// explicit class name. |
|
||||||
public $c = A::B; |
|
||||||
} |
} |
||||||
|
|
||||||
function main() |
namespace { |
||||||
{ |
function main() |
||||||
$test = new B; |
{ |
||||||
var_dump($test->a); |
$test = new StubConstRef\Child; |
||||||
var_dump($test->local); |
var_dump( |
||||||
var_dump($test->b); |
StubConstRef\Child::SELF_LOCAL, |
||||||
var_dump($test->c); |
StubConstRef\Child::SELF_INHERITED, |
||||||
|
StubConstRef\Child::PARENT_VALUE |
||||||
|
); |
||||||
|
var_dump( |
||||||
|
$test->selfLocal, |
||||||
|
$test->selfInherited, |
||||||
|
$test->parentValue, |
||||||
|
$test->explicitValue |
||||||
|
); |
||||||
|
var_dump($test->defaults()); |
||||||
|
var_dump((new StubConstRef\GrandChild)->runtimeStatic()); |
||||||
|
} |
||||||
} |
} |
||||||
?> |
?> |
||||||
--EXPECT-- |
--EXPECT-- |
||||||
|
int(10) |
||||||
int(1) |
int(1) |
||||||
|
int(2) |
||||||
int(10) |
int(10) |
||||||
int(1) |
int(1) |
||||||
int(2) |
int(2) |
||||||
|
int(1) |
||||||
|
array(2) { |
||||||
|
[0]=> |
||||||
|
int(10) |
||||||
|
[1]=> |
||||||
|
int(2) |
||||||
|
} |
||||||
|
int(30) |
||||||
|
|||||||
Loading…
Reference in new issue