Search code examples
phpclassobjectmodels

How to daisy chain php classes


I want to do this.

$ppl->tech->ceo->apple();

How would I make that work?


Solution

  • For example:

    class ppl {
      public $tech;
    
      public function __construct(){
        $this->tech = new tech();
      }
    }
    
    class tech {
      public $ceo;
    
      public function __construct(){
        $this->ceo = new ceo();
      }
    }
    
    class ceo {
      public function __construct(){
    
      }
    
      public function apple(){
        echo 'Hello.. I\'m apple.';
      }
    }