Preventing Inheritance and Overriding with final: PHP OOP

Video tutorial illustrating, how to prevent inheritance and overring using final keyword, in Object Oriented PHP.

stop-sign

In this program, we take two classes A and B, and then extend Class A to Class B.
Now using the final keyword, we’ll show how to avoid extending class A to class B or any other classes. Next, we’ll also show, how to use final keyword to prevent method overriding.

Avoiding Inheritance

<?php
 final class A
 {
 
 }
 
 class B extends A
 {
 
 }
?>

This through’s following fatal error:

Fatal error: Class B may not inherit from final class (A) in C:\wamp\www\OOP\final.php on line 16

So, using the keyword final infront of the class name prevents it from inheritance.

Avoiding Overriding of method

<?php
class A
{
final function display()
{
echo "Inside class A";
}
}
 
class B extends A
{
function display()
{
echo "Inside class B";
}
}
?>

This through’s following fatal error:

Fatal error: Cannot override final method A::display() in C:\wamp\www\OOP\final.php on line 16

So, using the keyword final infront of the method name prevents it from being overriden.

Related:
If you want to know the basics of Inheritance and Overriding, go through this short notes before proceeding
Single Inheritance: PHP OOP
Overriding: PHP OOP

Video Tutorial: Preventing Inheritance and Overriding with final: PHP OOP


[youtube https://www.youtube.com/watch?v=NXsD908RSVE]

YouTube Link: https://www.youtube.com/watch?v=NXsD908RSVE [Watch the Video In Full Screen.]


Sometimes preventing inheritance and avoiding overriding of certain methods becomes necessary inorder to maintain the sanity or data integrity while coding.

Overriding: PHP OOP

Video tutorial to illustrate Overriding in Object Oriented PHP Programming.

Here, we take two class: A and B. Extend class A to class B. Then, define method display() in class A.

Overriding is an object-oriented programming feature that enables a child class to provide different implementation for a method that is already defined and/or implemented in its parent class or one of its parent classes. The overriden method in the child class should have the same name, signature, and parameters as the one in its parent class.

Class B extends Class A

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< ?php
class A
{
function display()
{
echo "Inside Class A";
}
}
 
class B extends A
{
 
}
 
$obj = new B();
$obj->display();
?>

Next, redefine the same method display() inside class B too, with a different output.

Redefine method display inside class B

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
< ?php
class A
{
function display()
{
echo "Inside Class A";
}
}
 
class B extends A
{
function display()
{
echo "Inside Class B";
}
}
 
$obj = new B();
$obj->display();
?>

Now, the method inside class B has higher precedence over it’s parent class method.

Invoking parent method using Scope Resolution Operator

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
< ?php
class A
{
function display()
{
echo "Inside Class A";
}
}
 
class B extends A
{
function display()
{
      parent::display();
}
}
 
$obj = new B();
$obj->display();
?>

You can invoke parent method from subclass by using the keyword parent followed by scope resolution operator and the method name.

Overriding: PHP OOP


[youtube https://www.youtube.com/watch?v=5tcIUn6nbVE]

YouTube Link: https://www.youtube.com/watch?v=5tcIUn6nbVE [Watch the Video In Full Screen.]



Note:
1. Altering of function definition in the derived class does not alter the function definition in the parent class.
2. You can override both attributes and methods in the subclass.

Single Inheritance: PHP OOP

Inheritance is a way to reuse the code of existing objects.
In this video tutorial we’re illustrating single inheritance mechanism.

php-oop-single-inheritance

In this tutorial, we take a class called Vehicle with 3 attributes – color, accelerate and decelerate. And 3 methods – design, acceleration, deceleration.

Vehicle Class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
< ?php
class Vehicle
{
public $color;
public $accelerate;
public $decelerate;
 
function design($assign)
{
$this->color = $assign;
}
function acceleration($positive)
{
$this->accelerate = $positive;
}
function deceleration($negetive)
{
$this->decelerate = $negetive;
}
}
?>

Using the methods – design, acceleration and deceleration we assign values to the public attributes color, accelerate and decelerate.

Inherit Vehicle Class To Class Car
inheritance.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
< ?php
class Vehicle
{
public $color;
public $accelerate;
public $decelerate;
 
function design($assign)
{
$this->color = $assign;
}
function acceleration($positive)
{
$this->accelerate = $positive;
}
function deceleration($negetive)
{
$this->decelerate = $negetive;
}
}
 
class Car extends Vehicle
{
 
}
?>

Now create objects benze and bmw of class Car.

Objects of Class Car
vehicles.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< ?php
include_once("inheritance.php");
 
$benze = new Car();
$benze->design("Red");
echo $benze->color;
 
$bmw  = new Car();
$bmw->design("Silver");
echo "<br />".$bmw->color;
 
$benze->acceleration(20);
echo "<br />".$benze->accelerate;
 
$benze->deceleration(5);
echo "<br />".$benze->decelerate;
?>

Here, using the methods – design, acceleration and deceleration we assign attribute values to individual cars.

Single Inheritance: PHP OOP


[youtube https://www.youtube.com/watch?v=pdDqtHlkzEY]

YouTube Link: https://www.youtube.com/watch?v=pdDqtHlkzEY [Watch the Video In Full Screen.]



Uses of Inheritance:
Re-use of code.
Less and cleaner looking code.
Ease of understanding of code.
Ease of maintenance of project.

Access Specifiers: PHP OOP

Controlling Access with public, private and protected access modifiers.
These access specifiers can be used on both variables/attributes and methods.

In this video tutorial, we’ll demonstrate it using attributes.

Access Specifiers:
1. public
2. private
3. protected

Class With Attributes

1
2
3
4
5
6
7
8
9
< ?php
 class container
 {
 var $a       = 10;
public $b    = 20;
private $c   = 30;
protected $d = 40;
 }
?>

var $a is similar to writing public $a

We shall extend class container to another class called contains.
Extending class is an inheritance property, and we shall discuss inheritance in another article. For now, know that, with extends keyword, all the properties and methods with public / protected access are inherited.

Extends

1
2
3
4
5
6
7
8
9
10
11
12
13
< ?php
 class container
 {
 var $a       = 10;
public $b    = 20;
private $c   = 30;
protected $d = 40;
 }
 
 class contains extends container
 {
 }
?>

Here, contents of class container is extended by class contains.

Object of class container

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< ?php
 class container
 {
 var $a       = 10;
public $b    = 20;
private $c   = 30;
protected $d = 40;
 }
 
 class contains extends container
 {
 }
 
 $obj1 = new container();
         echo $obj1->a;
         echo $obj1->b;
?>

This outputs: 10 and 20 respectively.

Object of class contains

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< ?php
 class container
 {
 var $a       = 10;
public $b    = 20;
private $c   = 30;
protected $d = 40;
 }
 
 class contains extends container
 {
 }
 
 $obj2 = new contains();
         echo $obj2->a;
         echo $obj2->b;
?>

This outputs: 10 and 20 respectively.
i.e., public attributes are inherited

Accessing private / protected attributes outside class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< ?php
 class container
 {
 var $a       = 10;
public $b    = 20;
private $c   = 30;
protected $d = 40;
 }
 
 class contains extends container
 {
 }
 
 $obj1 = new container();
         echo $obj1->c;
         echo $obj1->d;
?>

It through’s error, since you cannot access, private and protected variables/attributes outside the class.

Now lets check if private and protected variables are actually inherited:

Inheriting private attribute

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
< ?php
 class container
 {
 var $a       = 10;
public $b    = 20;
private $c   = 30;
protected $d = 40;
 }
 
 class contains extends container
 {
 function __construct()
{
echo $this->c;
}
 }
 
 $obj1 = new contains();
?>

This through’s error, because $c is not inherited to class contains and thus not present inside class contains. Which means, private variables and methods cannot be inherited.

Inheriting protected attribute

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
< ?php
 class container
 {
 var $a       = 10;
public $b    = 20;
private $c   = 30;
protected $d = 40;
 }
 
 class contains extends container
 {
 function __construct()
{
echo $this->d;
}
 }
 
 $obj1 = new contains();
?>

Output’s 40.
This means, protected variables and methods are inherited.

Access Specifiers: PHP OOP


[youtube https://www.youtube.com/watch?v=2Ow_cgBEGmk]

YouTube Link: https://www.youtube.com/watch?v=2Ow_cgBEGmk [Watch the Video In Full Screen.]



Conclusion
public attributes and methods are inherited and can be accessed outside the class.
private attributes and methods cannot be inherited and cannot be accessed outside the class.
protected attributes and methods can be inherited but cannot be accessed outside the class.

Constructors and Destructors: PHP OOP

Video tutorial illustrates the use of Constructor and Destructor functions in Object Oriented PHP Programming.

Constructor: A constructor is an initialization routine having the same name as the class name, which is called automatically at the time of object creation.

Earlier to PHP5, constructor function had the same name as the class name. From PHP version 5, constructor functions has this special name __construct
For version compatibility, if we do not have a __construct method, it falls back and looks for the method with the same name as the class name.

Destructor: Destructor method is automatically called when all the references to a class have been unset or fallen out of scope.

Default Constructor:

1
2
3
4
5
6
7
8
9
10
       class Info
{
private $name;
 
function __construct()
{
$this->name = "Microsoft";
echo $this->name;
}
}

A class called Info, which has one private variable called $name;
a default constructor which assigns the value Microsoft to the variable $name and then echos it out.

Parameterized Constructor:

1
2
3
4
5
6
7
8
9
10
11
        class Info
{
private $name;
 
function __construct($text)
{
$this->name = $text;
echo $this->name;
}
      }
$a = new Info("Parameterized Constructor!");

Here we’re passing the parameter “Parameterized Constructor!”, which is then sent to __construct function which is intern assigned to the private variable $name and then echoed out.

Destructor:

1
2
3
4
5
6
7
8
9
10
11
class Info
{
private $name;
 
function __destruct()
{
 
echo "<br />Destructor called!"
       }
 
}

Note: Destructor method do not take parameter/arguments.

Full Code of Class File
info.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
< ?php
    class Info
{
private $name;
 
function __construct($text)
{
$this->name = $text;
echo $this->name;
}
 
function __destruct()
{
echo "<br />Destructor called!"
}
}
?>

Here we have Info class, with 1 private variable and a construct(parameterized) method and a destructor method.

Application File or the Code Processing
index.php

1
2
3
4
5
< ?php
include_once('info.php');
 
$a = new Info("Parameterized Constructor!");
?>

We include the class file(info.php) and then instantiate Info class.
Once it’s instantiated, __construct function is called and all the code inside it will be executed.
At the end of the scope of this class, __destruct method is called and all the code inside destruct method is executed.

Constructors and Destructors: PHP OOP


[youtube https://www.youtube.com/watch?v=xsOO06A8gxU]

YouTube Link: https://www.youtube.com/watch?v=xsOO06A8gxU [Watch the Video In Full Screen.]



Usually programmers connect to the database in the __construct method and then close the database connection inside __destruct method.
But you could configure your database info inside config.php file and then auto-load your database.

I would usually, initialize API’s inside my __construct method. Use the API output inside my other member methods and then release the resources inside __destruct method.