diff --git a/examples/property-hook.php b/examples/property-hook.php new file mode 100644 index 00000000..5eb5cbe8 --- /dev/null +++ b/examples/property-hook.php @@ -0,0 +1,24 @@ +modified) { + return $this->foo . ' (modified)'; + } + return $this->foo; + } + set(string $value) { + $this->foo = strtolower($value); + $this->modified = true; + } + } +} + +function main() +{ + $example = new Example(); + $example->foo = 'Hello World'; +} \ No newline at end of file diff --git a/examples/trait.php b/examples/trait.php index 9c7ee06c..2e8e4a81 100644 --- a/examples/trait.php +++ b/examples/trait.php @@ -3,6 +3,8 @@ trait TraitA { public function sayHello() { echo 'Hello'; + var_dump(__TRAIT__); + var_dump(__CLASS__); } } @@ -24,5 +26,8 @@ class MyHelloWorld } } -$obj = new MyHelloWorld(); -$obj->sayHelloWorld(); +function main() +{ + $obj = new MyHelloWorld(); + $obj->sayHelloWorld(); +} diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index 3d7fe2ad..36e53958 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -478,6 +478,7 @@ class CompilerBase extends \PhpAot\Core\Translator case 'Scalar_MagicConst_Function': case 'Scalar_MagicConst_Method': case 'Scalar_MagicConst_Class': + case 'Scalar_MagicConst_Trait': return $this->parseMagicConst($expr); case 'Scalar_InterpolatedString': return $this->parseInterpolatedString($expr); @@ -3472,6 +3473,17 @@ class CompilerBase extends \PhpAot\Core\Translator case 'Scalar_MagicConst_Function': return '"' . $this->escapeString($function) . '"'; case 'Scalar_MagicConst_Class': + if (!$this->classDef) { + $this->fatalError($expr, 'The magic constant `__CLASS__` is not allowed in global scope'); + } + if ($this->classDef->trait) { + return Symbol::getCalledClass(); + } + return '"' . $this->escapeString($class) . '"'; + case 'Scalar_MagicConst_Trait': + if (!$this->classDef or !$this->classDef->trait) { + $this->fatalError($expr, 'The magic constant `__TRAIT__` is not allowed in global scope'); + } return '"' . $this->escapeString($class) . '"'; case 'Scalar_MagicConst_Method': return '"' . $this->escapeString($class) . '::' . $this->escapeString($this->method) . '"'; diff --git a/src/Php/Preprocessor.php b/src/Php/Preprocessor.php index bf6ae712..d7dd2351 100644 --- a/src/Php/Preprocessor.php +++ b/src/Php/Preprocessor.php @@ -546,6 +546,9 @@ class Preprocessor extends CompilerBase protected function parseClassPropertyDef(Node\Stmt\Property $v): void { + if ($v->hooks and count($v->hooks) > 0) { + $this->fatalError($v, 'The class property hooks are not supported'); + } $oriCtx = $this->context; $this->context = $this->classDef->propertyContext; $flags = $this->parseModifiers($v->flags); diff --git a/tests/aot/trait/magic-const.phpt b/tests/aot/trait/magic-const.phpt new file mode 100644 index 00000000..9e306a90 --- /dev/null +++ b/tests/aot/trait/magic-const.phpt @@ -0,0 +1,28 @@ +--TEST-- +trait full class name +--FILE-- +sayHello(); + } +} + +function main() +{ + $obj = new MyHelloWorld(); + $obj->sayHelloWorld(); +} +?> +--EXPECT-- +string(6) "TraitA" +string(12) "MyHelloWorld" \ No newline at end of file