0

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

I know that $html is a subclass stored as a variable and not an inheritance. Any ideas?

Second question: Can I leave out the empty constructor in the Cache-Class?

3
  • "I have a core class as a collector and two subclasses stored in public variables in this core class" -- no, you don't. The Cache and Html classes are not subclasses of class Core. If you keep instances of them into Core::$cache and Core::$html they are collaborators. But in Core::__construct() you create two instances of Cache and Html and "keep" them in local variables that vanish when the function completes. Read more about PHP Classes and Objects and variables scope Commented Mar 21, 2017 at 12:07
  • thanks for your input, axiac. if the $core object exists the objects $html and $cache also will remain, right? So until the end of the request. And due to naming: collaborators is a real and right name for my solution? And why we don't talk about subclasses and name inherited classes child classes? That confuses me :-) Commented Mar 21, 2017 at 14:30
  • "subclass" and "child class" are two different names for the same concept: a class that extends another class. The exact name for your model (assuming you correctly set $this->cache and $this->html as suggested in the accepted answer) is "composition". A "collaborator" is an object passed as argument to a method call; the method delegates some work to the collaborator by calling one (or many) of its methods. It can be saved in a property for subsequent usage but this is not a requirement. Commented Mar 21, 2017 at 15:08

2 Answers 2

1

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. 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:

    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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

1 Comment

Good answer, I was thinking of this way but if there is no other chance :-) I named the $parent to $master otherwise it might be misleading. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.