Skip to content

Instantly share code, notes, and snippets.

@aqualad
Created May 29, 2013 22:27
Show Gist options
  • Save aqualad/5674347 to your computer and use it in GitHub Desktop.
Save aqualad/5674347 to your computer and use it in GitHub Desktop.
Example of Inheritance in PHP
<?php
class aParent {
public $a = 'a';
protected $b = 'b';
private $c = 'c';
}
class aChild extends aParent {
public function __construct()
{
$this->a .= 'a';
$this->b .= 'b';
$this->c .= 'c';
}
}
class aGrandChild extends aChild {
public function __construct()
{
$this->a .= 'a';
$this->b .= 'b';
$this->c .= 'c';
}
}
# GrandChild
$gc = new aGrandChild();
echo $gc->a; // Outputs 'aa'
// echo $gc->b; /* Throws an access error */
// echo $gc-c; /* Throws an access error */
# Child
$c = new aChild();
echo $c->a; // Outputs 'aa'
// echo $c->b; /* Throws an access error on variables, a protected */
/* method would be accessible */
// echo $c->c; /* Throws an access error */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment