V
V
Viktor Yanyshev2017-08-23 22:51:40
PHP
Viktor Yanyshev, 2017-08-23 22:51:40

How to pass a value to a property of an object in Method Fabic?

I am slowly learning patterns with the language, maybe somewhere I made a non-obvious mistake for myself. Why is $load always null?

abstract class Logistic {
    public $load;
    public $status;
    abstract function delivery();
}

abstract class Transport {
    abstract function uploaded();
    abstract function price();
}

class TruckDelivery extends Logistic {

    public function delivery()
    {
        if($this->load === 100) {
            $this->status =  "Отправка груза наземным транспортом \n";
        } elseif ($this->load < 100 && $this->load >= 90) {
            $this->status =  "Отправка груза наземным транспортом с добором груза по маршруту\n";
        } else  {
            $this->status = "Отправка груза невозможна. Наземный транспорт не загружен \n";
        }

        return $this->status;
    }

}

class AirplaneDelivery extends Logistic {

    public function delivery()
    {
        if($this->load === 100) {
            $this->status =  "Отправка груза воздушым грузовым судном \n";
        } else {
            $this->status = "Отправка груза невозможна. Воздушное судно не зангруженно \n";
        }

        return $this->status;
    }

}

class Airplane extends Transport {
    public function uploaded()
    {
        return new AirplaneDelivery();
    }

    public function price()
    {
        return "Стоимость 200 руб.\n";
    }
}

class Truck extends Transport {
    public function uploaded()
    {
        return new TruckDelivery();
    }

    public function price()
    {
        return "Стоимость 100 руб. + 50 руб. на загрузку попути\n";
    }
}

$shipping = new Truck();
$shipping->uploaded()->load = 100;
echo $shipping->uploaded()->delivery();
echo $shipping->price();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Night, 2017-08-23
@villiwalla

Your uploaded method always returns a new TruckDelivery instance, not a previously created one

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question