diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index d1a1bed4..062b3e24 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -1669,7 +1669,7 @@ class CompilerBase extends \PhpAot\Core\Translator } } - if (!$this->checkAccessible($classDef, $methodDef)) { + if (!$this->checkAccessible($classDef, $methodDef->flags)) { $this->fatalError($expr, 'Method `' . $classDef->getNamespacedName() . '::' . $method . '()` is not accessible'); } // 函数调用占位符,不是真实的函数调用 @@ -1703,7 +1703,7 @@ class CompilerBase extends \PhpAot\Core\Translator break; } } - if (!$this->checkAccessible($classDef, $constDef)) { + if (!$this->checkAccessible($classDef, $constDef->flags)) { $this->fatalError($expr, 'Constant `' . $classDef->getNamespacedName() . '::' . $const . '` is not accessible'); } if ($constDef->type === self::TYPE_ARRAY) { @@ -4199,7 +4199,7 @@ class CompilerBase extends \PhpAot\Core\Translator } } - protected function checkAccessible(ClassDef $classDef, MethodDef|ConstantDef $def): bool + protected function checkAccessible(ClassDef $classDef, int $flags): bool { // 在当前类中,允许调用所有方法 if ($classDef->namespace === $this->namespace and $classDef->name == $this->class) { @@ -4207,7 +4207,7 @@ class CompilerBase extends \PhpAot\Core\Translator } // 类外部调用,只允许调用 public 方法 - return $def->flags & Modifiers::PUBLIC; + return $flags & Modifiers::PUBLIC; } protected function isOverrideMethod(string $fullMethodName): bool diff --git a/src/Php/Entity/ConstantDef.php b/src/Php/Entity/ConstantDef.php index d8d4b402..8e91e5cc 100644 --- a/src/Php/Entity/ConstantDef.php +++ b/src/Php/Entity/ConstantDef.php @@ -12,11 +12,11 @@ class ConstantDef { public string $name; public string $type; - public string $flags; + public int $flags; public string $value; public string $arrayExpr; - public function __construct(string $name, string $flags, string $type, string $value, string $arrayExpr = '') + public function __construct(string $name, int $flags, string $type, string $value, string $arrayExpr = '') { $this->name = $name; $this->type = $type; diff --git a/src/Php/Translator.php b/src/Php/Translator.php index 0f8b3adc..632d1232 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -1106,7 +1106,7 @@ class Translator extends Preprocessor protected function parseClassConstDef(Node\Stmt\ClassConst $v): void { $this->resetFunction(); - $flags = $v->flags; + $flags = $this->parseModifiers($v->flags); if ($v->type) { $type = $this->parseTypeDecl($v->type, self::DECL_TYPE_OF_CONST); } else { @@ -1136,7 +1136,7 @@ class Translator extends Preprocessor protected function parsePropertyDef(Node\Stmt\Property $v): void { - $flags = $v->flags; + $flags = $this->parseModifiers($v->flags); $type = $this->parseTypeDecl($v->type, self::DECL_TYPE_OF_PROPERTY); foreach ($v->props as $prop) { @@ -1151,15 +1151,19 @@ class Translator extends Preprocessor } } - protected function parseClassMethod(Node\Stmt\ClassMethod $v, array &$methodCodes): void + protected function parseModifiers(int $flags): int { - $name = $this->getMethodName($v); - $this->method = $name; - - $flags = $v->flags; if (!($flags & Modifiers::PRIVATE) and !($flags & Modifiers::PROTECTED)) { $flags |= Modifiers::PUBLIC; } + return $flags; + } + + protected function parseClassMethod(Node\Stmt\ClassMethod $v, array &$methodCodes): void + { + $name = $this->getMethodName($v); + $this->method = $name; + $flags = $this->parseModifiers($v->flags); if (!($flags & Modifiers::ABSTRACT)) { $this->methodDef = new MethodDef($flags, $name);