Created
February 8, 2018 13:32
-
-
Save cgrymala/637cd4fae75037b9b201e7c23bf95f1c to your computer and use it in GitHub Desktop.
Showing how PHP handles inheritance
This file contains 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 | |
class A { | |
function __construct() { | |
$this->sample_method(); | |
} | |
private function sample_method() { | |
echo "<p>I am inside the parent class 'A'</p>"; | |
} | |
} | |
class B extends A { | |
function __construct() { | |
parent::__construct(); | |
} | |
private function sample_method() { | |
echo "<p>I am inside the child class 'B'</p>"; | |
} | |
} | |
$b = new B; // This will use the A::sample_method() method | |
class C { | |
function __construct() { | |
$this->sample_method_c(); | |
} | |
public function sample_method_c() { | |
echo "<p>I am inside the parent class 'C'</p>"; | |
} | |
} | |
class D extends C { | |
function __construct() { | |
parent::__construct(); | |
} | |
public function sample_method_c() { | |
echo "<p>I am inside the child class 'D'</p>"; | |
} | |
} | |
$d = new D; // This will use the D::sample_method_c() method |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment