fix(php): 修复类方法访问权限检查逻辑

- 重构 checkAccessible 方法,正确处理 private 和 protected 方法的访问控制
- 添加对受保护方法的继承关系检查
- 修复当前类内部访问权限判断逻辑
- 优化 findNativeMethod 中的对象方法查找流程
- 添加缺失的返回值处理逻辑
pull/1/head
韩天峰 4 months ago
parent 69a1c24610
commit 492a1dbd39
  1. 32
      src/Php/CompilerBase.php
  2. 28
      tests/aot/ref/007.phpt

@ -4593,13 +4593,16 @@ class CompilerBase extends \PhpAot\Core\Translator
protected function checkAccessible(ClassDef $classDef, int $flags): bool
{
// 在当前类中,允许调用所有方法
if ($classDef->namespace === $this->namespace and $classDef->name == $this->class) {
return true;
// 私有方法,只能当前的类使用
if ($flags & Modifiers::PRIVATE) {
return $classDef->namespace === $this->namespace and $classDef->name == $this->class;
}
// 保护方法,只能当前类和子类使用
if ($flags & Modifiers::PROTECTED) {
return $this->isInheritedFrom($this->classDef->getNamespacedName(), $classDef->getNamespacedName());
}
// 类外部调用,只允许调用 public 方法
return $flags & Modifiers::PUBLIC;
return true;
}
protected function isOverrideMethod(string $fullMethodName): bool
@ -4610,21 +4613,24 @@ class CompilerBase extends \PhpAot\Core\Translator
protected function findNativeMethod(CallLike $expr, string $object, string $method): string|false
{
$nativeFunc = '';
$classDef = null;
if ($object === 'this_') {
$nativeFunc = $this->getNativeName($method, $this->namespace, $this->class);
$class = $this->class;
$classDef = $this->classDef;
} elseif (isset($this->context->objects[$object])) {
$class = $this->context->objects[$object];
$nativeFunc = $this->getNativeMethod($expr, $class, $method);
// 存在 Native 类,但是没有找到方法,可能是动态调用
if (!$nativeFunc) {
if ($this->hasClass($class) and $this->getNativeMethod($expr, $class, '__call', false)) {
throw new DynamicCall();
}
} else {
return false;
}
$nativeFunc = $this->getNativeMethod($expr, $class, $method);
// 存在 Native 类,但是没有找到方法,可能是动态调用
if (!$nativeFunc) {
if ($this->hasClass($class) and $this->getNativeMethod($expr, $class, '__call', false)) {
throw new DynamicCall();
}
}
if ($classDef) {
$fullMethodName = $classDef->getNamespacedName(false) . '::' . $method;
} else {

@ -0,0 +1,28 @@
--TEST--
class const 001
--FILE--
<?php
class WorkerA
{
protected function foo(string $name, string &$value) {
$value = 'hello ' . $name;
}
}
class WorkerB extends WorkerA
{
public function bar(string $name) {
$value = '';
$this->foo($name, $value);
return $value;
}
}
function main()
{
$o = new WorkerB;
var_dump($o->bar('php'));
}
?>
--EXPECT--
string(9) "hello php"
Loading…
Cancel
Save