I
I
Ilya Bychek2021-06-07 21:12:57
PHP
Ilya Bychek, 2021-06-07 21:12:57

Passing parameter in inherited classes?

Hello. When I pass parameters to addMilk and addEggs, there is nothing in the output.
In this case, everything that is transmitted, there are numbers.

What's wrong?

<?php

class Farm extends Store{

    public $animals = [];

    public function addAnimal($animal)
    {
    	if ($animal instanceOf Animal) {
    		$this->animals[] = $animal;
    	}
    }

    public function collectProducts()
    {
        foreach ($this->animals as $animal)
        {
            if ($animal instanceOf CanGiveMilk) {
                $milkLiters = $animal->collectMilk();
                $this->addMilk($milkLiters);
            }

            if ($animal instanceOf CanGiveEggs) {
                $eggsCount = $animal->collectEgg();
                $this->addEggs($eggsCount);
            }
        }
    }
}


class Store{

    public $milkLiters = 0;
    public $eggsCount = 0;

    public function addMilk($liters)
    {
        $this->milkLiters += $liters;
    }

    public function addEggs($eggsCount)
    {
        $this->eggsCount += $eggsCount;

    }
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question