Advertisement
brandizzi

Not creating property

Oct 25th, 2016
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.57 KB | None | 0 0
  1. <?php
  2. // cf. http://stackoverflow.com/questions/40230045/why-should-i-declare-the-class-property-in-php
  3. class bird{
  4.     public function __construct($canFly, $canWalk, $legCount){
  5.         $this->canFly = $canFly;
  6.         if ($canWalk) {
  7.             $this->legCount = $legCount;
  8.         }
  9.     }
  10.     public function getLegCount(){
  11.         return $this->legCount;
  12.     }
  13. }
  14.  
  15. $b1 = new bird(true, true, 3);  // $this->legCount created here
  16. $b2 = new bird(true, false, 3); // $this->legCount NOT created here
  17.  
  18. echo $b1->getLegCount() . "\n";
  19. echo $b2->getLegCount() . "\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement