title: Call to a member function on a non-object in
body: <p>I am getting my head around the oo PHP and get stuck with this annoying thing.</p>
  
  <p>I am trying to build a model of different types of lamps and lightbulps and I guess everything is nearly fine apart from the fact that the Fatal error "Call to a member function zapal() on a non-object in C:\xampp\htdocs\excersises\oo\lampki.php on line 9"</p>
  
  <p>Here is the code and after that my way of thinking, so please correct me where I am wrong</p>
  
  <pre><code>&lt;?php
  class Lampa {
    protected $zarowka;
    public function _construct (Zarowka $zarowka) {
      echo 'Wkrecam .arówke&lt;br&gt;';
      $this-&gt;zarowka = $zarowka;
    }
    public function zapal() {
      $this-&gt;zarowka-&gt;zapal();
      echo 'Lampka zapalona&lt;br&gt;';
    }
    public function zgas() {
      $this-&gt;zarowka-&gt;zgas();
      echo 'Lampka zgaszona&lt;br&gt;';
    }
  }
  
  class SciemniaczZarowki {
    private $zarowka;
    private $poziomJasnosci = 100;
    public function _construct (Zarowka $zarowka) {
      $this-&gt;zarowka = $zarowka;
    }
    public function rozjasnij() {
      if ($this-&gt;poziomJasnosci &lt; 100) {
        $this-&gt;poziomJasnosci++;
        echo 'Rozjasniam swiatlo zarowki&lt;br&gt;';
      }
    }
    public function sciemnij() {
      if ($this-&gt;poziomJasnosci &gt; 1) {
        $this-&gt;poziomJasnosci--;
        echo 'Przyciemniam swiatlo zarowki&lt;br&gt;';
      }
    }
  }
  
  class LampkaNocna extends Lampa{
      private $sciemniacz;
  
  
           public function _construct (Zarowka $zarowka) {
               parent::_construct($zarowka);
  
          $this-&gt;sciemniaczZarowki = new SciemniaczZarowki($this-&gt;zarowka);
    }
    public function rozjasnij() {
      $this-&gt;sciemniaczZarowki-&gt; rozjasnij();
    }
    public function sciemnij() {
      $this-&gt;sciemniaczZarowki-&gt; sciemnij();
    }
  }
  
  // Dodane przez mnie
  class Zarowka{
      public $kolor;
      function __construct(){
          echo "Jam zarówka";
          $this-&gt;kolor = "biały";
      }
      private function zapal(){
          echo "zarówka: Świace!";
      }
      private function zgas(){
          echo "zarówka: Gasne!";
      }
  
  }
  
  $zarowka = new Zarowka();
  $lampka = new LampkaNocna($zarowka);
  $lampka-&gt;zapal();
  $lampka-&gt;rozjasnij();
  $lampka-&gt;sciemnij();
  $lampka-&gt;sciemnij();
  $lampka-&gt;rozjasnij();
  $lampka-&gt;rozjasnij();
  $lampka-&gt;rozjasnij();
  $lampka-&gt;zgas();
  ?&gt;
  </code></pre>
  
  <p>Since this representation does not show line numbers let me break it into parts. The function that causes the error is in Lampa class:</p>
  
  <pre><code>public function zapal() {
      $this-&gt;zarowka-&gt;zapal();
      echo 'Lampka zapalona&lt;br&gt;';
    }
  </code></pre>
  
  <p>My thinking always was that since I have Zarowka class that has a method I later call in that line it should be ok. What is more before creating a new Lampa object I already have an object Zarowka, so why it says it is non-object problem (since in </p>
  
  <pre><code>$lampka = new LampkaNocna($zarowka);
  </code></pre>
  
  <p>i pass it into Lampa)?</p>
  
  <p>I bet that the solution is simple and easy but I guess I have been staring at this code for too long.
  If you decide to answer and have some time please could you describe your thinking maybe in a step by step flow so the future readers can take advantage of it.</p>