diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index cd16c9fc..492bbb0b 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -3270,11 +3270,13 @@ class CompilerBase extends \PhpAot\Core\Translator } if ($argInfo->type === self::TYPE_OBJECT) { - $object = $this->parseVariable($arg->value); - if ($this->isVarExpr($arg->value) and $this->isTypedObject($object)) { - $class = $this->getObjectType($object); - if ($argInfo->class and $class != $argInfo->class) { - $this->fatalError($arg, "Argument `{$argInfo->name}` must be an instance of `{$argInfo->class}`, `{$class}` given"); + if ($this->isVarExpr($arg->value)) { + $object = $this->parseVariable($arg->value); + if ($this->isTypedObject($object)) { + $class = $this->getObjectType($object); + if ($argInfo->class and $class != $argInfo->class) { + $this->fatalError($arg, "Argument `{$argInfo->name}` must be an instance of `{$argInfo->class}`, `{$class}` given"); + } } } return $this->convertObjectExpr($expr); diff --git a/src/Php/Reflection.php b/src/Php/Reflection.php index 2158e8ac..833c3213 100644 --- a/src/Php/Reflection.php +++ b/src/Php/Reflection.php @@ -95,6 +95,21 @@ class Reflection return $args[$index]; } + public static function getClassMethodModifiers(string $className, string $fn): ?int + { + $classRef = self::getClass($className); + if (!$classRef) { + return null; + } + + try { + $method = $classRef->getMethod($fn); + return $method->getModifiers(); + } catch (\ReflectionException $e) { + return null; + } + } + public static function getClassMethodParameter(string $className, string $fn, int $index): ?\ReflectionParameter { $classRef = self::getClass($className); diff --git a/src/Php/Translator.php b/src/Php/Translator.php index f6995ac3..e0ec0874 100644 --- a/src/Php/Translator.php +++ b/src/Php/Translator.php @@ -1200,10 +1200,18 @@ class Translator extends Preprocessor if (!$extends) { break; } + // 父类是内置类 + if ($classDef->inheritedFromInternalClass) { + if (Reflection::getClassMethodModifiers($extends, $name) & \ReflectionMethod::IS_PRIVATE) { + goto _error; + } + break; + } $classDef = $this->getClass($extends); if ($classDef->hasMethod($this->method)) { $methodDef = $classDef->getMethod($this->method); if ($methodDef->flags & Modifiers::PRIVATE) { + _error: $this->fatalError($v, 'Cannot override private method `' . $classDef->getNamespacedName(false) . '::' . $this->method . '()`');