- 新增 001.phpt 测试基础匿名类声明功能 - 新增 002.phpt 测试匿名类继承和实现接口功能 - 新增 003.phpt 测试匿名类重复使用场景 - 新增 004.phpt 测试匿名类继承机制 - 修改编译器基类中类名获取逻辑,支持反斜杠开头的命名空间类名 - 修复函数中声明类的错误检查逻辑 - 改进变量检测逻辑以避免未定义变量错误 - 修复类定义代码中的数组初始化逻辑 - 为类名添加反斜杠前缀以确保正确引用 - 添加静态调用注释以改善调试信息 - 调整枚举测试文件中函数位置以符合预期结构pull/1/head
parent
8ec0723ed5
commit
35a81376a1
6 changed files with 136 additions and 8 deletions
@ -0,0 +1,11 @@ |
|||||||
|
--TEST-- |
||||||
|
declare bare anonymous class |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
function main() { |
||||||
|
var_dump(new class{}); |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECTF-- |
||||||
|
object(%s)#%d (0) { |
||||||
|
} |
||||||
@ -0,0 +1,22 @@ |
|||||||
|
--TEST-- |
||||||
|
declare anonymous class extending another |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
class A{} |
||||||
|
|
||||||
|
interface B{ |
||||||
|
public function method(); |
||||||
|
} |
||||||
|
|
||||||
|
function main() { |
||||||
|
$a = new class extends A implements B { |
||||||
|
public function method(){ |
||||||
|
return true; |
||||||
|
} |
||||||
|
}; |
||||||
|
var_dump($a instanceof A, $a instanceof B); |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
bool(true) |
||||||
|
bool(true) |
||||||
@ -0,0 +1,55 @@ |
|||||||
|
--TEST-- |
||||||
|
reusing anonymous classes |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
$i = 0; |
||||||
|
while ($i++<10) { |
||||||
|
var_dump(new class($i) { |
||||||
|
public $i; |
||||||
|
public function __construct($i) { |
||||||
|
$this->i = $i; |
||||||
|
} |
||||||
|
}); |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECTF-- |
||||||
|
object(%s)#1 (1) { |
||||||
|
["i"]=> |
||||||
|
int(1) |
||||||
|
} |
||||||
|
object(%s)#1 (1) { |
||||||
|
["i"]=> |
||||||
|
int(2) |
||||||
|
} |
||||||
|
object(%s)#1 (1) { |
||||||
|
["i"]=> |
||||||
|
int(3) |
||||||
|
} |
||||||
|
object(%s)#1 (1) { |
||||||
|
["i"]=> |
||||||
|
int(4) |
||||||
|
} |
||||||
|
object(%s)#1 (1) { |
||||||
|
["i"]=> |
||||||
|
int(5) |
||||||
|
} |
||||||
|
object(%s)#1 (1) { |
||||||
|
["i"]=> |
||||||
|
int(6) |
||||||
|
} |
||||||
|
object(%s)#1 (1) { |
||||||
|
["i"]=> |
||||||
|
int(7) |
||||||
|
} |
||||||
|
object(%s)#1 (1) { |
||||||
|
["i"]=> |
||||||
|
int(8) |
||||||
|
} |
||||||
|
object(%s)#1 (1) { |
||||||
|
["i"]=> |
||||||
|
int(9) |
||||||
|
} |
||||||
|
object(%s)#1 (1) { |
||||||
|
["i"]=> |
||||||
|
int(10) |
||||||
|
} |
||||||
@ -0,0 +1,33 @@ |
|||||||
|
--TEST-- |
||||||
|
testing anonymous inheritance |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
class Outer { |
||||||
|
protected $data; |
||||||
|
|
||||||
|
public function __construct($data) { |
||||||
|
$this->data = $data; |
||||||
|
} |
||||||
|
|
||||||
|
public function getArrayAccess() { |
||||||
|
/* create a proxy object implementing array access */ |
||||||
|
return new class($this->data) extends Outer implements ArrayAccess { |
||||||
|
public function offsetGet($offset): mixed { return $this->data[$offset]; } |
||||||
|
public function offsetSet($offset, $data): void { $this->data[$offset] = $data; } |
||||||
|
public function offsetUnset($offset): void { unset($this->data[$offset]); } |
||||||
|
public function offsetExists($offset): bool { return isset($this->data[$offset]); } |
||||||
|
}; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
function main() { |
||||||
|
$outer = new Outer(array( |
||||||
|
rand(1, 100) |
||||||
|
)); |
||||||
|
|
||||||
|
/* not null because inheritance */ |
||||||
|
var_dump($outer->getArrayAccess()[0]); |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECTF-- |
||||||
|
int(%d) |
||||||
Loading…
Reference in new issue