PHP Inheritance
Inheritance is a programming principle to accesses parent class’s properties and methods in child class. This concept is well-established on the object model.
When you create a chain object inheritance, then this principle affects all another class’s properties & methods, which relates to the current class.
For Exampleample you created a sub-class and This sub-class inherits a parent class, then this sub-class overrides parent class’s properties and method when you created same-named properties or methods.
Note:
Unless autoloading is used, then classes must be defined before they are used. If a class Exampletends another, then the parent class must be declared before the child class structure. This rule applies to classes that inherit other classes and interfaces.
How to implement inheritance
<?php
class Foo
{
public function printItem($string)
{
echo 'Foo: ' . $string . PHP_EOL;
}
public function printPHP()
{
echo 'PHP is great.' . PHP_EOL;
}
}
class Bar extends Foo
{
public function printItem($string)
{
echo 'Bar: ' . $string . PHP_EOL;
}
}
$foo = new Foo();
$bar = new Bar();
$foo->printItem('baz'); // Output: 'Foo: baz'
$foo->printPHP(); // Output: 'PHP is great'
$bar->printItem('baz'); // Output: 'Bar: baz'
$bar->printPHP(); // Output: 'PHP is great'
?>
Scope resolution operator
The scope resolution operator(::) is a special operator in PHP, which used to access static methods, constants properties to override properties & methods of a class.
This operator can be used both-side inside & outside of a class. It indicates a double colon (::).
Example-1: How to access properties inside of a class
<?php
class OtherClass extends MyClass
{
public static $my_static = 'static var';
public static function doubleColon() {
echo parent::CONST_VALUE . "\n";
echo self::$my_static . "\n";
}
}
$classname = 'OtherClass';
$classname::doubleColon(); // As of PHP 5.3.0
OtherClass::doubleColon();
?>
Example-2: How to access properties outside of a class
<?php
class MyClass {
const CONST_VALUE = 'A constant value';
}
$classname = 'MyClass';
echo $classname::CONST_VALUE; // As of PHP 5.3.0
echo MyClass::CONST_VALUE;
?>
How to access parent class method by scope resolution operator
<?php
class MyClass
{
protected function myFunc() {
echo "MyClass::myFunc()\n";
}
}
class OtherClass extends MyClass
{
// Override parent's definition
public function myFunc()
{
// But still call the parent function
parent::myFunc();
echo "OtherClass::myFunc()\n";
}
}
$class = new OtherClass();
$class->myFunc();
?>
Static keyword
the static keyword is a special keyword that is used to make static properties or methods of a class. This keyword also used to make static variables.
In a class, a static keyword is used to access properties or methods of a class with creating an instance of the object. the pseudo-variable $this is not available inside the method declared as static.
Caution:
In PHP 5, calling non-static methods statically generates an E_STRICT level warning.
Warning:
In PHP 7, calling non-static methods statically is deprecated, and will generate an E_DEPRECATED warning. Support for calling non-static methods statically may be removed in the future.
How to use static keyword in a class
<?php
class Foo {
public static function aStaticMethod() {
// ...
}
}
Foo::aStaticMethod();
$classname = 'Foo';
$classname::aStaticMethod(); // As of PHP 5.3.0
?>