brandizzi icon

Not creating property

brandizzi | PRO | 10/25/16 05:44:14 PM UTC | 0 ⭐ | 670 👁️ | Never ⏰ | []
PHP |

570 B

|

None

|

0 👍

/

0 👎

<?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