ccurtin
7/30/2015 - 1:06 AM

PHP-Accessor-Crash-Course

PHP-Accessor-Crash-Course


/*
 *
 *   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.