po pierwsze źle definiujesz składową 'type' klasy Element
zamiast
private type = 'default';
powinno być
private $type = 'default';
w metodzie save() ponownie źle przekazujesz parametr.
zamiast:
public function save(){
$this->type = 'red';
box::storeElement(this); //tutaj jak zwrócić instancje obiektu Element ?
}
powinno być
public function save(){
$this->type = 'red';
box::storeElement($this); //tutaj jak zwrócić instancje obiektu Element ?
}
Kolejnym błędem jest odwoływanie się do prywatnej składowej 'type' klasy Element w sposób jaki to robimy dla publicznych składowych.
public static function storeElement
($element){ if ($element->type == 'red' ) self::goToRedElements(); // type jest prywatna!
else self::goToDefaultElements();
}
Jeśli chcesz mieć dostęp tylko do odczytu wartości 'type' musisz dodać metodę do klasy Element zwracającą tę wartość.
np:
{
return $this->type;
}
Poniżej wklejam poprawnie napisany kod:
<?php
class Box
{
public static function storeElement
($element){ if ($element->getType() == 'red') {
self::goToRedElements();
} else {
self::goToDefaultElements();
}
}
}
class Element
{
private $type = 'default';
{
return $this->type;
}
public function save(){
$this->type = 'red';
box::storeElement($this); //tutaj jak zwrócić instancje obiektu Element ?
}
}
/**************************/
$element = new Element();
$element->save();
//nie chce tego robić tak:
$element = new Element();
$element->save();
//z metody save() wylatuje wywołanie box::storeElement();
box::storeElement($element);
?>