PHP 伪造多重继承 - 在扩展类中可用的伪造父类中设置对象属性

发布于 2024-10-01 17:16:58 字数 1571 浏览 1 评论 0原文

我已经使用了伪造的多重继承,如 我可以在 PHP 中使用 1 个以上的类来扩展一个类吗?

请注意,A 类实际上扩展了 B 类,并且伪造了从 C 类扩展的方法。

在我需要一个属性之前,它工作得很好。在类 C 的函数中设置在类 A 中可用。考虑该代码的稍微编辑版本,其中我从类 A 的函数内部调用类 C 的函数:-

//Class A
class A extends B
{
  private $c;

  public function __construct()
  {
    $this->c = new C;
  }

  // fake "extends C" using magic function
  public function __call($method, $args)
  {
    return call_user_func_array(array($this->c, $method), $args);
  }

//calling a function of class C from inside a function of class A
  public function method_from_a($s) {
     $this->method_from_c($s);
     echo $this->param; //Does not work
  }

//calling a function of class B from inside a function of class A
  public function another_method_from_a($s) {
     $this->method_from_b($s);
     echo $this->another_param; //Works
  }
}

//Class C

class C {
    public function method_from_c($s) {
        $this->param = "test";
    }
}

//Class B
class B {
    public function method_from_b($s) {
        $this->another_param = "test";
    }
}


$a = new A;
$a->method_from_a("def");
$a->another_method_from_a("def");

因此,在类 C 的函数中设置的属性之后在 A 类中不可用,但如果在 B 类中设置,则在 A 类中可用。为了使假父类中的属性设置像真实的一样工作,我缺少什么调整?像正常情况一样,假父函数中设置的属性应该在层次结构的所有类中可用。

谢谢

已解决
我在 A 类中添加了神奇的函数 __get() 并且它起作用了。

public function __get($name)
{   
   return $this->c->$name;
}

I have used faking of multiple inheritance as given in Can I extend a class using more than 1 class in PHP?

Notice that class A actually extends class B and faking is done for extending from class C.

It was working fine until I needed an attribute set in a function of class C to be available in class A. Consider a little edited version of that code where I call a function of class C from inside a function of class A :-

//Class A
class A extends B
{
  private $c;

  public function __construct()
  {
    $this->c = new C;
  }

  // fake "extends C" using magic function
  public function __call($method, $args)
  {
    return call_user_func_array(array($this->c, $method), $args);
  }

//calling a function of class C from inside a function of class A
  public function method_from_a($s) {
     $this->method_from_c($s);
     echo $this->param; //Does not work
  }

//calling a function of class B from inside a function of class A
  public function another_method_from_a($s) {
     $this->method_from_b($s);
     echo $this->another_param; //Works
  }
}

//Class C

class C {
    public function method_from_c($s) {
        $this->param = "test";
    }
}

//Class B
class B {
    public function method_from_b($s) {
        $this->another_param = "test";
    }
}


$a = new A;
$a->method_from_a("def");
$a->another_method_from_a("def");

So, an attribute set in a function of class C is not available afterwards in class A but if set in class B, it is available in class A. What adjustment am I missing so as to make setting of attributes in the fake parent class work like real? An attribute set in fake parent's function should be available in all the classes of the hierarchy like in normal case.

Thanks

Solved
I added the magic function __get() in class A and it worked.

public function __get($name)
{   
   return $this->c->$name;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

星星的軌跡 2024-10-08 17:16:58

这永远不会起作用,因为 'param' 不是 A 的属性:它在 c 中,而 c 是 A 的属性。

您需要做的是定义 神奇方法,例如 __set 和 __get,它们与属性的 __call 并行。

That will never work, because 'param' is not a property of A: it is in c, which is a property of A.

What you need to do is define the magic methods such as __set and __get, which parallel __call for properties.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文