diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index d49f1280..1b16db49 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -593,6 +593,10 @@ class CompilerBase extends \PhpAot\Core\Translator protected function getNamespacedClassName(string $class): string { + if ($class[0] === '\\') { + return $class; + } + $ns2 = explode('\\', trim($class, '\\')); if (isset($this->useAliases[$ns2[0]])) { @@ -971,7 +975,6 @@ class CompilerBase extends \PhpAot\Core\Translator break; case 'Stmt_Class': $this->fatalError($v, 'Cannot declare class in function'); - break; default: abort($v); } @@ -1980,7 +1983,7 @@ class CompilerBase extends \PhpAot\Core\Translator } } elseif ($this->isArrayDimFetch($arg->value)) { $array = $this->parseIdentifier($arg->value->var); - if (!$this->hasVar($array)) { + if ($this->isVarExpr($arg->value->var) and !$this->hasVar($array)) { $this->fatalError($arg, 'Undefined variable `$' . $array . '`'); } if ($funcName and Reflection::isReferenceArg($funcName, $i)) { @@ -2354,6 +2357,7 @@ class CompilerBase extends \PhpAot\Core\Translator $out .= "\n\t"; } } + $out .= '0,'; return $out; } @@ -2375,6 +2379,7 @@ class CompilerBase extends \PhpAot\Core\Translator $this->addConstData($className . '_code', $classCode); $this->beforeStmtLines[] = 'if (!' . $className . '_defined) {' . $className . '_defined = true; php::eval((const char *)' . $className . '_code);}'; + $className = '\\' . $className; } else { $this->fatalError($expr, 'must be anonymous class'); } @@ -3161,6 +3166,8 @@ class CompilerBase extends \PhpAot\Core\Translator return $this->parseParentMethodCall($expr); } + $this->beforeStmtLines[] = '// Static Call: ' . $class . '::' . $method; + if ($this->isNameExpr($expr->class) and $this->isIdExpr($expr->name)) { $nativeFunc = $this->getNativeStaticMethod($class, $method); if ($nativeFunc) { diff --git a/tests/aot/enum2.phpt b/tests/aot/enum2.phpt index ce382f0e..3b8cb2fb 100644 --- a/tests/aot/enum2.phpt +++ b/tests/aot/enum2.phpt @@ -2,14 +2,14 @@ enum 2 --FILE-- diff --git a/tests/zend/anon/001.phpt b/tests/zend/anon/001.phpt new file mode 100644 index 00000000..dac987ba --- /dev/null +++ b/tests/zend/anon/001.phpt @@ -0,0 +1,11 @@ +--TEST-- +declare bare anonymous class +--FILE-- + +--EXPECTF-- +object(%s)#%d (0) { +} diff --git a/tests/zend/anon/002.phpt b/tests/zend/anon/002.phpt new file mode 100644 index 00000000..b172ea35 --- /dev/null +++ b/tests/zend/anon/002.phpt @@ -0,0 +1,22 @@ +--TEST-- +declare anonymous class extending another +--FILE-- + +--EXPECT-- +bool(true) +bool(true) diff --git a/tests/zend/anon/003.phpt b/tests/zend/anon/003.phpt new file mode 100644 index 00000000..741ee67f --- /dev/null +++ b/tests/zend/anon/003.phpt @@ -0,0 +1,55 @@ +--TEST-- +reusing anonymous classes +--FILE-- +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) +} diff --git a/tests/zend/anon/004.phpt b/tests/zend/anon/004.phpt new file mode 100644 index 00000000..772dfec4 --- /dev/null +++ b/tests/zend/anon/004.phpt @@ -0,0 +1,33 @@ +--TEST-- +testing anonymous inheritance +--FILE-- +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)