Last active
August 29, 2015 14:26
-
-
Save ccurtin/23637d489c8cb2c8b110 to your computer and use it in GitHub Desktop.
PHP-Accessor-Crash-Course
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * | |
| * PHP Accessors | |
| * | |
| */ | |
| // 'protected' and 'private' can ONLY be accessed from WITHIN the classes themselves. NOT OUTSIDE. | |
| // EX: instantiating an object then calling a protected or private method or property will throw an error. | |
| // Only 'public' methods and properties can be accessed OUTSIDE the classes themselves | |
| class parent_Class | |
| { | |
| protected $protected_Name; | |
| private $private_Name; | |
| function __construct(){ | |
| $this->protected_Name = " private_Property "; | |
| $this->private_Name = " public_Property "; | |
| } | |
| // any class can access 'public' | |
| public function showPublic(){ | |
| return $this->private_Name; | |
| } | |
| // any extending class can access 'protected' | |
| protected function showProtected(){ | |
| return $this->private_Name; | |
| } | |
| // only the class calling 'private' can use it. | |
| private function showPrivate(){ | |
| return $this->private_Name; | |
| } | |
| } | |
| class Child extends parent_Class | |
| { | |
| function __construct(){ | |
| // parent must be constructed to access its values but NOT needed to create NEW values. | |
| parent::__construct(); | |
| // modify the parent's private var | |
| $this->protected_Name = strtolower($this->protected_Name); | |
| } | |
| public function childShowPublic(){ | |
| return parent::showPublic(); // call parents 'public' method. | |
| } | |
| public function childShowProtected(){ | |
| return parent::showProtected(); // call the parent 'protected' method | |
| } | |
| /* | |
| public function showProtected(){ | |
| return "<br> I will overwrite the parent function called showProtected() unless parent:: is called"; | |
| } | |
| */ | |
| public function childShowPrivate(){ | |
| return parent::showPrivate(); // call the parents 'private' method | |
| } | |
| } | |
| $topLevel = new parent_Class; | |
| // OK: 'public' methods CAN return 'private' variables | |
| echo $topLevel->showPublic(); | |
| // ERROR(fatal): can't directly call a 'protected' method. A child 'public' method CAN call a parents 'protected' method or property but not 'private' method/property | |
| echo $topLevel->showProtected(); | |
| // ERROR(fatal): can't directly call a 'private' method. A child 'public' method CANNOT call a parents 'private' method or property. | |
| echo $topLevel->showPrivate(); | |
| // all the following Child methods have 'public' accessors that access 'public','protected',and 'private' method from the Parent class. | |
| $objectWantsToKnow = new Child; | |
| // OK: inherits the parents 'public' method. CAN call a 'private' variable | |
| echo $objectWantsToKnow->childShowPublic(); // OR... | |
| echo $objectWantsToKnow->showPublic(); // only 'public' methods can be called without calling parent:: | |
| // OK: 'protected' methods are shared among Parent and Child classes. | |
| echo $objectWantsToKnow->childShowProtected(); | |
| // ERROR(fatal): can't access a parents 'private' method. Ancestors of a class CANNOT access 'private' methods or properties. | |
| echo $objectWantsToKnow->childShowPrivate(); | |
| // ERROR(notice): can't access a parents 'private' property. Ancestors of a class CANNOT access 'private' methods or properties. | |
| echo $objectWantsToKnow->private_Name; // won't return anything. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment