- 修改 parseAssignPropertyFetch 方法,添加 literal 参数到 identifierToStr 调用 - 在条件判断中增加对 nativeProperty 属性的检查,区分原生属性和魔术方法调用 - 更新 parseUnset 方法中的属性名解析逻辑,统一使用 identifierToStr 并添加 literal 参数 - 重构 getPropertyIdentifier 方法参数列表,增加 expr 参数并设置 nativeProperty 属性 - 移除类属性不存在时的致命错误抛出,允许魔术方法处理未知属性访问 - 添加完整的魔术方法测试用例,覆盖 __get/__set/__isset/__unset 的基本功能pull/1/head
parent
45e3933b3c
commit
23f2fa48af
2 changed files with 72 additions and 16 deletions
@ -0,0 +1,57 @@ |
||||
--TEST-- |
||||
Magic Methods - __get, __set |
||||
--SKIPIF-- |
||||
--FILE-- |
||||
<?php |
||||
class MagicClass { |
||||
public array $data = []; |
||||
public string $propStr = 'default value'; |
||||
|
||||
// __get and __set |
||||
public function __get(string $name): mixed { |
||||
return $this->data[$name] ?? null; |
||||
} |
||||
|
||||
public function __set(string $name, mixed $value): void { |
||||
$this->data[$name] = $value; |
||||
} |
||||
|
||||
// __isset and __unset |
||||
public function __isset(string $name): bool { |
||||
return isset($this->data[$name]); |
||||
} |
||||
|
||||
public function __unset(string $name): void { |
||||
unset($this->data[$name]); |
||||
} |
||||
} |
||||
|
||||
function main() { |
||||
$magic = new MagicClass(); |
||||
|
||||
// Test __set and __get |
||||
$magic->property = 'test value'; |
||||
var_dump($magic->property); |
||||
|
||||
$magic->propStr = 'new value'; |
||||
var_dump($magic->propStr); |
||||
|
||||
$magic->number = 42; |
||||
var_dump($magic->number); |
||||
|
||||
// Test __isset and __unset |
||||
var_dump(isset($magic->property)); |
||||
var_dump(isset($magic->nonexistent)); |
||||
|
||||
unset($magic->property); |
||||
var_dump(isset($magic->property)); |
||||
} |
||||
?> |
||||
--EXPECT-- |
||||
string(10) "test value" |
||||
string(9) "new value" |
||||
int(42) |
||||
bool(true) |
||||
bool(false) |
||||
bool(false) |
||||
|
||||
Loading…
Reference in new issue