<?php
// cf. http://stackoverflow.com/questions/40230045/why-should-i-declare-the-class-property-in-php
class bird{
public function __construct($canFly, $canWalk, $legCount){
$this->canFly = $canFly;
if ($canWalk) {
$this->legCount = $legCount;
}
}
public function getLegCount(){
return $this->legCount;
}
}
$b1 = new bird(true, true, 3); // $this->legCount created here
$b2 = new bird(true, false, 3); // $this->legCount NOT created here
echo $b1->getLegCount() . "\n";
echo $b2->getLegCount() . "\n";
Comments