diff --git a/examples/error/call-str-method.php b/examples/error/call-str-method.php new file mode 100644 index 00000000..c39b9b02 --- /dev/null +++ b/examples/error/call-str-method.php @@ -0,0 +1,15 @@ +call(); +} + +function main() +{ + foo("hello"); +} diff --git a/src/Php/CompilerBase.php b/src/Php/CompilerBase.php index cd9012e5..c6611f48 100644 --- a/src/Php/CompilerBase.php +++ b/src/Php/CompilerBase.php @@ -4137,6 +4137,10 @@ class CompilerBase extends \PhpAot\Core\Translator // 可转为原生调用的 MethodCall if ($this->isVarExpr($expr->var) and $this->isNamedMethod($expr->name)) { + $type = $this->getVarType($object); + if (!$this->checkArgType($type, self::TYPE_OBJECT)) { + $this->fatalError($expr, 'Cannot call method `' . $expr->name->toString() . '()` on variable of type ' . $type); + } $this->context->beforeStmtLines[] = '// Method Call: ' . $object . '->' . $this->parseIdentifier($expr->name) . '()'; try { $nativeFunc = $this->findNativeMethod($expr, $object, $this->parseIdentifier($expr->name)); diff --git a/src/Php/Reflection.php b/src/Php/Reflection.php index e2b1a3a7..e05dd2c0 100644 --- a/src/Php/Reflection.php +++ b/src/Php/Reflection.php @@ -177,14 +177,14 @@ class Reflection public static function getMethodReturnType(string $class, string $method): ?string { - $class = self::getClass($class); - if (!$class) { + $classRef = self::getClass($class); + if (!$classRef) { return null; } - if (!$class->hasMethod($method)) { + if (!$classRef->hasMethod($method)) { return null; } - $method = $class->getMethod($method); - return $method->getReturnType() ? $method->getReturnType()->getName() : null; + $methodDef = $classRef->getMethod($method); + return $methodDef->getReturnType() ? $methodDef->getReturnType()->getName() : null; } }