简体   繁体   English

php访问子类的父对象(无继承)

[英]php access parent object of a subclass (no inheritance)

I have a core class as a collector and two subclasses stored in public variables in this core class: 我有一个核心类作为收集器,并且有两个子类存储在此核心类的公共变量中:

class Core
{
  public $cache;
  public $html;

  public function __construct()
  {
    $cache = new Cache();
    $html  = new Html();
  }
}

class Cache
{
  public function __construct()
  {
  }

  public function store($value)
  {
    // do something
  }
}

class Html
{
  public $foo;

  public function __construct()
  {
    $foo = "bar";
    global $core;
    $core->cache->store($foo);
  }

}

QUESTION: I would like to get rid of the line "global $core" and do something like: $this->parent->cache->store($foo) $cache should be connected to $core in some way because it is a public member of $core 问题:我想摆脱“ global $ core”这一行,并做类似的事情:$ this-> parent-> cache-> store($ foo)$ cache应该以某种方式连接到$ core,因为它是$ core的公共成员

I know that $html is a subclass stored as a variable and not an inheritance. 我知道$ html是存储为变量而不是继承的子类。 Any ideas? 有任何想法吗?

Second question: Can I leave out the empty constructor in the Cache-Class? 第二个问题:是否可以在Cache-Class中忽略空的构造函数?

What you can do is to use the concept of dependency injection to inject in your HTML class an object of the class Cache, and then, use it to call method store. 您可以做的是使用依赖项注入的概念在HTML类中注入Cache类的对象,然后使用它来调用方法存储。 I believe that this is a very good approach. 我相信这是一个非常好的方法。 So you can do something like this. 所以你可以做这样的事情。

class Core
{
  public $cache;
  public $html;

  public function __construct()
  {
    $cache = new Cache();
    $html  = new Html($cache);
  }
}

In your class HTML: 在您的类HTML中:

    class Html
    {
      public $foo;

      public function __construct(Cache $cache)
      {
        $foo = "bar";
        $cache->store($foo);
      }
    }

About your second question, if there is no necessity of do something in the constructor, you could just ommit it. 关于第二个问题,如果不需要在构造函数中做任何事情,则可以忽略它。 But there is no problem to let it empty as well. 但是也没有问题让它也为空。 So I think that it up to you. 所以我认为这取决于您。

Your object can't access caller class methods, because he do not know anything about it's caller. 您的对象无法访问调用者类的方法,因为他对该调用者一无所知。

You can try to pass parent when creating new object 您可以在创建新对象时尝试传递父对象

class Core {
   public $cache;
   public $html;

   public function __construct() {
       $this->cache = new Cache($this);
       $this->html = new Html($this);
   }
}

class Html {
    public $parent;

    public function __construct($parent) {
        $this->parent = $parent;

        if (!empty($this->parent->cache)) {
            $this->parent->cache->store();
        }
    }
}

Can I leave out the empty constructor - yes, you even do not have to declare __construct method at all, as all classes has it's default constructor/destructor. 我可以省略空的构造函数吗?是的,您甚至根本不必声明__construct方法,因为所有类都具有默认的构造函数/析构函数。

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

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