Skip to content

Instantly share code, notes, and snippets.

@cgrymala
Created February 8, 2018 13:32
Show Gist options
  • Save cgrymala/637cd4fae75037b9b201e7c23bf95f1c to your computer and use it in GitHub Desktop.
Save cgrymala/637cd4fae75037b9b201e7c23bf95f1c to your computer and use it in GitHub Desktop.
Showing how PHP handles inheritance
<?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