Fatal error: Can’t use method return value in write context in …

Filed in Bez kategorii Leave a comment

Fatal error: Can’t use method return value in write context in …
Błąd php który występuję w przypadku gdy chcemy sprawdzić przy pomocy funkcji isset() czy dana zmienna nie zawiera null.

Na potrzeby wyjaśnienia problemu prosta klasa Car, z 1 zmienną i 2 metodami.

$car = new Car();
$car->isPainted();

class Car {

  private $color;

  function setColor($color) {
     $this->color = $color;
  }

  function getColor() {
     return $this->color;
  }
}

Teraz  dodajemy funkcję isPainted() która powoduje błąd

Nie należy jej stosować, jest niepoprawna. Dobry przykład znajduje się niżej.

function isPainted() {
     if (isset($this->getColor())) {
      return true;
    } else {
      return false;
    }
  }

Teraz jak należy ją zaimplementować by działała.

function isPainted() {
     if ($this->getColor() != null) {
      return true;
    } else {
      return false;
    }
  }

lub metoda niepolecana, aczkolwiek również działająca

function isPainted() {
     if (isset($this->color)) {
      return true;
    } else {
      return false;
    }
  }

,

TOP