1

I am getting my head around the oo PHP and get stuck with this annoying thing.

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"

Here is the code and after that my way of thinking, so please correct me where I am wrong

<?php
class Lampa {
  protected $zarowka;
  public function _construct (Zarowka $zarowka) {
    echo 'Wkrecam .arówke<br>';
    $this->zarowka = $zarowka;
  }
  public function zapal() {
    $this->zarowka->zapal();
    echo 'Lampka zapalona<br>';
  }
  public function zgas() {
    $this->zarowka->zgas();
    echo 'Lampka zgaszona<br>';
  }
}

class SciemniaczZarowki {
  private $zarowka;
  private $poziomJasnosci = 100;
  public function _construct (Zarowka $zarowka) {
    $this->zarowka = $zarowka;
  }
  public function rozjasnij() {
    if ($this->poziomJasnosci < 100) {
      $this->poziomJasnosci++;
      echo 'Rozjasniam swiatlo zarowki<br>';
    }
  }
  public function sciemnij() {
    if ($this->poziomJasnosci > 1) {
      $this->poziomJasnosci--;
      echo 'Przyciemniam swiatlo zarowki<br>';
    }
  }
}

class LampkaNocna extends Lampa{
    private $sciemniacz;


         public function _construct (Zarowka $zarowka) {
             parent::_construct($zarowka);

        $this->sciemniaczZarowki = new SciemniaczZarowki($this->zarowka);
  }
  public function rozjasnij() {
    $this->sciemniaczZarowki-> rozjasnij();
  }
  public function sciemnij() {
    $this->sciemniaczZarowki-> sciemnij();
  }
}

// Dodane przez mnie
class Zarowka{
    public $kolor;
    function __construct(){
        echo "Jam zarówka";
        $this->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->zapal();
$lampka->rozjasnij();
$lampka->sciemnij();
$lampka->sciemnij();
$lampka->rozjasnij();
$lampka->rozjasnij();
$lampka->rozjasnij();
$lampka->zgas();
?>

Since this representation does not show line numbers let me break it into parts. The function that causes the error is in Lampa class:

public function zapal() {
    $this->zarowka->zapal();
    echo 'Lampka zapalona<br>';
  }

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

$lampka = new LampkaNocna($zarowka);

i pass it into Lampa)?

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.

tereško
  • 58,060
  • 25
  • 98
  • 150
Adam P
  • 139
  • 1
  • 10

1 Answers1

3

In your example code, your constructors are named _construct(), with a single leading underscore.

In PHP, the correct constructor name is __construct(), with two leading underscores. In other words, your constructors are never being called - so the variable that you're passing is not actually getting stored in the class(es).

Update Lampa (and all other classes) to have a constructor similar to:

public function __construct (Zarowka $zarowka) {
    ....
newfurniturey
  • 37,556
  • 9
  • 94
  • 102