PHP: How PHP is inconsistent


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. <?php
  2. /**********************
  3. Class Example
  4. **********************/
  5. class Car{
  6. //Inconsistency with access modifiers
  7. var $tire_count = 4;
  8. public $door_count = 2;
  9.  
  10. function num_of_tires(){
  11. //You need to explicitely use $ to assign variables but then don't use them for reference
  12. $string = "This " . get_class($this) . " has " . $this->tire_count . " tires";
  13. return $string . "<br />";
  14. }
  15. }
  16.  
  17. $my_car = new Car();
  18. echo $my_car->tire_count . " :: " . $my_car->door_count . "<br />";
  19. echo $my_car->num_of_tires();
  20.  
  21. /**********************
  22. Global Variable Example
  23. **********************/
  24. //No use of var or public
  25. $tires = 4;
  26. $doors = 2;
  27.  
  28. echo $tires . " :: " . $doors . "<br />";
  29. //echo $this->$cat;
  30.  
  31. //Reference looks ugly
  32. $reference =& $tires;
  33. echo "Reference before: " . $reference . " || ";
  34. $tires = 2;
  35. echo "Reference after: " . $reference;
  36. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.