Advertisement
zenware

PHP Class Tutorial

Jan 27th, 2013
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.71 KB | None | 0 0
  1. <?php
  2.  
  3. class Tutorial {
  4.     /**
  5.      * Variables inside a class are accessed by referencing the class and the variable.
  6.      * Example: $this->publicString, $this->protectedString, $this->privateString
  7.      */
  8.     public $publicString = 'Anything can access this!';
  9.     protected $protectedString = 'This can be accessed by sub-classes!';
  10.     private  $privateString = 'This can't be accessed from anywhere else!';
  11.  
  12.     /**
  13.      * If a constuctor takes arguments, they are given at initialization.
  14.      * Example: $tutorial = new Tutorial($arguments);
  15.      * Note: There are some interestingly fancy ways to handle args in constructors.
  16.      */
  17.     public function __construct() {
  18.         print('This code executes when you initialize or "construct" the class.');
  19.         $this->publicScope();
  20.         $this->protectedScope();
  21.         $this->privateScope();
  22.     }
  23.  
  24.     // Full External Accessibility
  25.     public function publicScope()
  26.     {
  27.         print('This function can be accessed anywhere.');
  28.     }
  29.  
  30.     // Partial External Accessibility
  31.     protected function protectedScope()
  32.     {
  33.         print('This function can only be accessed from within the class, or subclasses.');
  34.     }
  35.  
  36.     // No External Accessibility
  37.     private function privateScope()
  38.     {
  39.         print('This function can only be accessed from within the class.');
  40.     }
  41.  
  42.  
  43. }
  44.  
  45. /**
  46. * If a class extends another class, it has access to all the functions in the parent class.
  47. * And it can alter the functions from the parent class.
  48. */
  49. class ExtendedTutorial extends Tutorial
  50. {
  51.     public function __construct($someData)
  52.     {
  53.         /**
  54.          * Using the 'parent::' prefix directly references parent values.
  55.          * It's generally used to have two functions with the same name accessible in the same scope.
  56.          */
  57.         parent::__construct();
  58.  
  59.         print($someData);
  60.     }
  61.  
  62.     public function publicScope()
  63.     {
  64.         print($this->publicString);
  65.     }
  66.  
  67.     protected function protectedScope()
  68.     {
  69.         print($this->protectedString);
  70.     }
  71.  
  72.     private function privateScope()
  73.     {
  74.         print($this->privateString);
  75.     }
  76.  
  77. }
  78.  
  79. /**
  80.  * Initializes the Tutorial Class to the variable $tut.
  81.  * Since all the functions in the Tutorial Class are executed in the constructor they will run immediately.
  82.  */
  83. $tut = new Tutorial();
  84.  
  85. //$tut->publicScope(); # Success
  86. //$tut->privateScope(); # Failure, Can only be called within the class or subclasses.
  87. //$tut->protectedScope(); # Failure, Can only be called within the class.
  88.  
  89. /**
  90.  * Yay Subclass initialization.
  91.  * This can access all the public and protected members of the parent class.
  92.  */
  93. $extTut = new ExtendedTutorial("This is going to print after the parent constructor executes.");
  94.  
  95. # Reference Initialized Variable, Call publicScope(); function
  96. $extTut->publicScope();
  97. # Will Fail
  98. #$extTut->protectedScope();
  99. #$extTut->privateScope();
  100. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement