Object Oriented PHP
Topics going to cover: -
1. Access Modifiers in PHP
2. Method overloading
3. Inheritance
4. Calling parent constructors
5. Function Overriding
Restricting access to properties using 'access
modifiers'
One of the fundamental principles in OOP is 'encapsulation'. The idea is that you create
cleaner better code, if you restrict access to the data structures (properties) in your objects.
You restrict access to class properties using something called 'access modifiers'.
There are 3 access modifiers:
• public
• private
• protected
public is the default modifier in PHP.
Method overloading
PHP provides the __call() method to handle the concept of method overloading. This magic
method is fired when the code attempts to call for methods that are either not accessible due
to the scope or do not exist.
<?php
Class MyMath{
public $a=0;
public $b=0;
public function __construct($a, $b){
$this->a = $a;
$this->b = $b;
}
public function add(){
return $this->a + $this->b."\n";
}
public function __call($method, $arguments){
return "A function with name: ".$method." does not exist\n";
}
}
$math = new MyMath(5,6);
echo $math->add();
echo $math->subtract();
?>
Inheritance
PHP class definitions can optionally inherit from a parent class definition by using the extends
clause. The syntax is as follows −
class Child extends Parent {
<definition body>
}
The effect of inheritance is that the child class (or subclass or derived class) has the following
characteristics −
• Automatically has all the member variable declarations of the parent class.
• Automatically has all the same member functions as the parent, which (by default) will
work the same way as those functions do in the parent.
Following example inherit Books class and adds more functionality based on the requirement.
class Novel extends Books {
var $publisher;
function setPublisher($par){
$this->publisher = $par;
}
function getPublisher(){
echo $this->publisher. "<br />";
}
}
Calling parent constructors
Instead of writing an entirely new constructor for the subclass, let's write it by calling the
parent's constructor explicitly and then doing whatever is necessary in addition for
instantiation of the subclass. Here's a simple example −
/*Super class*/
class Name {
var $_firstName;
var $_lastName;
function __construct ($first_name, $last_name) {
$this->_firstName = $first_name;
$this->_lastName = $last_name;
}
function toString() {
return($this->_lastName .", " .$this->_firstName);
}
}
/*Sub Class*/
class NameSub1 extends Name {
var $_middleInitial;
function __construct ($first_name, $middle_initial, $last_name) {
Name::Name($first_name, $last_name);
$this->_middleInitial = $middle_initial;
}
function toString() {
return(Name::toString() . " " . $this->_middleInitial);
}
}
Function Overriding
Function definitions in child classes override definitions with the same name in parent
classes. In a child class, we can modify the definition of a function inherited from parent class.
In the following example getPrice and getTitle functions are overridden to return some values.
function getPrice() {
echo $this->price . "<br/>";
return $this->price;
}
function getTitle(){
echo $this->title . "<br/>";
return $this->title;
}