×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Dmitriy Lanets
Added: May 15, 2015 6:52 AM
Views: 5
Tags: no tags
  1. class User {
  2.     const GIVEN = 1;  // class constants can't be labeled static nor assigned visibility
  3.     public $a=2;
  4.     public static $b=3;
  5.  
  6.     public function me(){
  7.         echo "print me";
  8.     }
  9.      public static function you() {
  10.         echo "print you";
  11.     }
  12. }
  13.  
  14. class myUser extends User {
  15. }
  16.  
  17. // Are properties and methods instantiated to an object of a class, & are they accessible?
  18. //$object1= new User();        // uncomment this line with each of the following lines individually
  19. //echo $object1->GIVEN . "</br>";        // yields nothing
  20. //echo $object1->GIVE . "</br>";        //  deliberately misnamed, still yields nothing
  21. //echo $object1->User::GIVEN . "</br>";    // yields nothing
  22. //echo $object1->a . "</br>";        // yields 2
  23. //echo $object1->b . "</br>";        // yields nothing
  24. //echo $object1->me() . "</br>";        // yields print me
  25. //echo $object1->you() . "</br>";        // yields print you
  26.  
  27. // Are  properties and methods instantiated to an object of a child class,  & are accessible?
  28. //$object2= new myUser();        // uncomment this line with each of the following lines individually
  29. //echo $object2->GIVEN . "</br>";        // yields nothing
  30. //echo $object2->a . "</br>";        // yields 2
  31. //echo $object2->b . "</br>";        // yields nothing
  32. //echo $object2->me() . "</br>";        // yields print me
  33. //echo $object2->you() . "</br>";        // yields print you
  34.  
  35. // Are the properties and methods accessible directly in the class?
  36. //echo User::GIVEN . "</br>";        // yields 1
  37. //echo User::$a . "</br>";            // yields fatal error since it is not static
  38. //echo User::$b . "</br>";            // yields 3
  39. //echo User::me() . "</br>";        // yields print me
  40. //echo User::you() . "</br>";        // yields print you
  41.  
  42. // Are the properties and methods copied to the child class and are they accessible?
  43. //echo myUser::GIVEN . "</br>";        // yields 1
  44. //echo myUser::$a . "</br>";        // yields fatal error since it is not static
  45. //echo myUser::$b . "</br>";        // yields 3
  46. //echo myUser::me() . "</br>";        // yields print me
  47. //echo myUser::you() . "</br>";        // yields print you