From 66249916f56cb44e0ea4f15fe0c37131f0362701 Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Fri, 16 Jan 2026 17:23:22 +0800 Subject: [PATCH] =?UTF-8?q?fix(php):=20=E4=BF=AE=E5=A4=8D$this=E5=8F=98?= =?UTF-8?q?=E9=87=8F=E9=87=8D=E6=96=B0=E8=B5=8B=E5=80=BC=E6=A3=80=E6=9F=A5?= =?UTF-8?q?=E5=B9=B6=E6=B7=BB=E5=8A=A0=E7=B1=BB=E5=8A=9F=E8=83=BD=E6=B5=8B?= =?UTF-8?q?=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加对$this变量重新赋值的错误检查,防止Cannot re-assign $this错误 - 新增destructor异常处理测试用例验证析构函数中的异常捕获 - 添加工厂模式测试用例验证对象创建功能 - 新增final方法测试用例验证最终方法重写规则 - 添加abstract与final冲突测试用例验证编译时错误处理 - 新增__toString与__destruct交互测试用例验证字符串转换功能 --- src/Php/CompilerBase.php | 3 + .../classes/destructor_and_exceptions.phpt | 57 +++++++++++++++++++ tests/core/classes/factory_001.phpt | 33 +++++++++++ tests/core/classes/final.phpt | 30 ++++++++++ tests/core/classes/final_abstract.phpt | 16 ++++++ tests/core/classes/tostring_002.phpt | 26 +++++++++ 6 files changed, 165 insertions(+) create mode 100644 tests/core/classes/destructor_and_exceptions.phpt create mode 100644 tests/core/classes/factory_001.phpt create mode 100644 tests/core/classes/final.phpt create mode 100644 tests/core/classes/final_abstract.phpt create mode 100644 tests/core/classes/tostring_002.phpt diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 5e002d75..484d7f19 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -775,6 +775,9 @@ class CompilerBase extends \PhpAot\Core\Translator return $code . '}'; } $var = $this->parseIdentifier($left); + if ($var === 'this_') { + $this->fatalError($left, 'Cannot re-assign $this'); + } $expr = $this->parseExpr($right); if (!$this->hasVar($var)) { diff --git a/tests/core/classes/destructor_and_exceptions.phpt b/tests/core/classes/destructor_and_exceptions.phpt new file mode 100644 index 00000000..9d67ebb7 --- /dev/null +++ b/tests/core/classes/destructor_and_exceptions.phpt @@ -0,0 +1,57 @@ +--TEST-- +ZE2 catch exception thrown in destructor +--FILE-- +getMessage() . "\n"; + } + try + { + throw new FatalException("Damn"); + } + catch(Exception $e) + { + echo "Caught Exception: " . $e->getMessage() . "\n"; + } + catch(FatalException $e) + { + echo "Caught FatalException: " . $e->getMessage() . "\n"; + } +} +?> +--EXPECT-- +FailClass::__destruct +Caught: FailClass +FatalException::__construct +FailClass::__destruct +Caught Exception: FailClass diff --git a/tests/core/classes/factory_001.phpt b/tests/core/classes/factory_001.phpt new file mode 100644 index 00000000..988345aa --- /dev/null +++ b/tests/core/classes/factory_001.phpt @@ -0,0 +1,33 @@ +--TEST-- +ZE2 factory objects +--FILE-- +draw(); + ShapeFactoryMethod("Square")->draw(); +} +?> +--EXPECT-- +Circle +Square diff --git a/tests/core/classes/final.phpt b/tests/core/classes/final.phpt new file mode 100644 index 00000000..95c2028e --- /dev/null +++ b/tests/core/classes/final.phpt @@ -0,0 +1,30 @@ +--TEST-- +ZE2 A method may be redeclared final +--FILE-- +show(); + $t2 = new second(); + $t2->show(); + + echo "Done\n"; +} +?> +--EXPECT-- +Call to function first::show() +Call to function second::show() +Done diff --git a/tests/core/classes/final_abstract.phpt b/tests/core/classes/final_abstract.phpt new file mode 100644 index 00000000..a6d391e7 --- /dev/null +++ b/tests/core/classes/final_abstract.phpt @@ -0,0 +1,16 @@ +--TEST-- +ZE2 A final method cannot be abstract +--SKIPIF-- + +--FILE-- + +--EXPECTF-- +Fatal error: Cannot use the final modifier on an abstract method in %s on line %d diff --git a/tests/core/classes/tostring_002.phpt b/tests/core/classes/tostring_002.phpt new file mode 100644 index 00000000..6b1edb7a --- /dev/null +++ b/tests/core/classes/tostring_002.phpt @@ -0,0 +1,26 @@ +--TEST-- +ZE2 __toString() in __destruct +--FILE-- + +--EXPECT-- +Hello +Hello