简体   繁体   English

如何在PHP中访问包含对象的属性?

[英]how to access containing object's properties in PHP?

I'm writing some PHP code in which one object (a "Container") keeps a pointer to another object (the "Contents"). 我正在编写一些PHP代码,其中一个对象(“容器”)保存指向另一个对象(“内容”)的指针。 The problem is that the contents needs to access methods or properties of the container. 问题是内容需要访问容器的方法或属性。

Here's a simplified example of what I want to do: 这是我想要做的简化示例:

class Container {
    function __construct($type, $contents) {
        $this->type = $type;
        $this->contents = $contents;
    }

    function display() {
        return $this->contents->display();
    }
}

class Contents {
    function __construct($stuff) {
        $this->stuff = $stuff;
    }

    function display() {
        return 'I am ' . $this->stuff . ' in '; // how to access Container here?
    }
}

$item = new Container('a can', new Contents('Prince Albert'));
echo $item->display() . "\n";
// Displays: I am Prince Albert in 
// Wanted: I am Prince Albert in a can

What's the right way to do this? 这样做的正确方法是什么?

I've tried a couple of methods that work, but they feel wrong. 我尝试了几种有效的方法,但他们感觉不对。 For example: 例如:

  • Re-defined Contents::display() to take a parameter, which doesn't seem elegant: 重新定义Contents::display()来获取一个看起来不优雅的参数:

     function display($container) { return 'I am ' . $this->stuff . ' in ' . $container->type; } 
  • In Contents::display() , I called debug_backtrace(true) to find out what called it, then access the object from the backtrace info. Contents::display() ,我调用了debug_backtrace(true)来查找调用它的内容,然后从backtrace信息中访问该对象。 That feels kludgy/dangerous. 这感觉很危险/危险。

Is there a common solution for this kind of problem? 这种问题有一个共同的解决方案吗?

At all there are two common solution. 总之有两种常见的解决方案。 The one is the first one you already mention 这是你提到的第一个

class A {
  public function doSomething ($outer) { /* code */ }
}

where $outer is your container. 其中$outer是你的容器。 Or you strictly bind the content objects to the container 或者您严格将内容对象绑定到容器

class A {
  private $outer;
  public function __construct ($outer) {
    $this->outer = $outer;
  }
}

With dependency injection, you would construct the Container first (not passing in a Contents): 使用依赖项注入,您将首先构造Container(而不是传入Contents):

class Container {
    function __construct($type) {

Then, you would pass the Container to the Contents constructor: 然后,您将Container传递给Contents构造函数:

class Contents {
    function __construct($stuff, $container) {

Since the reference is mutual, you would have to call a setter on container: 由于引用是相互的,因此您必须在容器上调用setter:

class Container {
    function setContents($contents)

Try this: 试试这个:

class Container
{
    protected $type;
    protected $contents;

    function __construct($type, Contents $contents)
    {
        $this->type = $type;
        $this->contents = $contents;
        $contents->setContainer($this);
    }

    function display()
    {
        return $this->contents->display();
    }

    public function getType()
    {
        return $this->type;
    }
}

class Contents
{
    /** @var Container */
    protected $container;

    function __construct($stuff)
    {
        $this->stuff = $stuff;
    }

    public function setContainer(Container $container)
    {
        $this->container = $container;
    }

    function display()
    {
        return 'I am '.$this->stuff.' in '.$this->container->getType(); // how to access Container here?
    }
}

$item = new Container('a can', new Contents('Prince Albert'));
echo $item->display()."\n";
// Displays: I am Prince Albert in
// Wanted: I am Prince Albert in a can

And as advice: write public/protected/private for each method and variables, don't use public properties. 作为建议:为每个方法和变量编写public / protected / private,不要使用公共属性。 If you don't know why, read this book: http://www.amazon.com/Objects-Patterns-Practice-Matt-Zandstra/dp/1590599098 如果您不知道原因,请阅读本书: http//www.amazon.com/Objects-Patterns-Practice-Matt-Zandstra/dp/1590599098

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM