class Bird{
    public $canFly;
    public $legCount;
    public $className;
    
    public function __construct($canFly, $legCount, $className) {
        $this->canFly = $canFly;
        $this->legCount = $legCount;
        $this->className = $className;
    }
    
    public function canFly() {
        return $this->canFly;
    }
    
    public function getLegCount() {
        return $this->legCount;
    }
    
    public function getBirdInfo() {
        $canFly = $this->canFly() ? 'can fly' : 'can not fly';
        return $this->className. ' ' . $canFly . ' and has ' . $this->getLegCount() . ' legs' . PHP_EOL;
    }
}

class Pigeon extends Bird{
    public function __construct($canFly, $legCount) {
        parent::__construct($canFly, $legCount, get_class());
    }
}

class Penguin extends Bird{
    public function __construct($canFly, $legCount) {
        parent::__construct($canFly, $legCount, get_class());
    }
}

$pigeon = new Pigeon(true, 2);
$penguin = new Penguin(false, 2);

echo $pigeon->getBirdInfo();
echo $penguin->getBirdInfo();
by Valeri Tandilashvili
4 years ago
PHP
OOP
PHP Object Oriented Programming (OOP)
2
Pro tip: use ```triple backticks around text``` to write in code fences