PHP Object Oriented Programming
PHP Object Oriented Programming
1
KAZI A S M-9765645688
2
KAZI A S M-9765645688
3
KAZI A S M-9765645688
Note the cow inherits from the animal class and defines its own variable and methods too.
Let’s now code the Cow class
<?php
class Cow extends Animal
{
private $owner;
public function __construct($family, $food)
{
parent::__construct($family, $food);
}
public function set_owner($owner)
{
$this->owner = $owner;
}
public function get_owner()
{
return $this->owner;
}
}
?>
Let’s now code the Lion class
<?php
class Lion extends Animal
{
public function __construct($family, $food)
4
KAZI A S M-9765645688
{
parent::__construct($family, $food);
}
}
?>
HERE,
“class … extends Animal” makes the cow and lion use methods from the Animal class
(Inheritance).
How to Create object of the class
The Animal, Cow, and Lion classes should all be in the same directory for simplicity’s sake.
Let’s now create the application that uses our classes.
PHP Class Example
<?php
require 'Animal.php';
require 'Cow.php';
require 'Lion.php';
$cow = new Cow('Herbivore', 'Grass');
$lion = new Lion('Canirval', 'Meat');
echo '<b>Cow Object</b> <br>';
echo 'The Cow belongs to the ' . $cow->get_family() . ' family and eats ' . $cow->get_food() . '<b
r><br>';
echo '<b>Lion Object</b> <br>';
echo 'The Lion belongs to the ' . $lion->get_family() . ' family and eats ' . $lion->get_food();
?>
.
Fantastic right! Let’s now look at the third principle of OOP, polymorphism.
Let’s say we want to develop an application that connects to different database engines such as
MySQL and SQL Server but use the same uniform interface.
We can create an interface that defines the standard methods and an abstract class that
implements the common methods.
Interface – it is similar to a class. It only defines the methods and parameters.
Abstract class – it is a class that cannot be used to create an object directly. Its purpose is
to provide partial or whole implementations of common methods.
The class diagram below illustrates the relationship among our abstract class, interface, and
implementation classes.
5
KAZI A S M-9765645688
6
KAZI A S M-9765645688
}
?>
HERE,
“abstract class” means the class cannot be used directly to php create object
“$host,$db…” are class variables common to all implementations
“function __construct(…)” is the php class constructor method that sets the common
variables values at initialization
Let’s now create the interface that contains the standard methods which will be implemented
differently depending on the database engine.
<?php
interface DBInterface
{
public function db_connect();
public function insert($data);
public function read($where);
public function update($where);
public function delete($where);
}
?>
HERE,
“interface” is the keyword for creating interfaces
“public function…(…)” are the standard methods that should be implemented
Let’s now create the concrete classes that will extend the DBCommonMethods class and extend
the DBInterface interface. MySQLDriver.php
<?php class MySQLDriver extends
DBCommonMethods implements DBInterface { public function __construct($host, $db, $uid, $p
assword)
{
parent::__construct($host, $db, $uid, $password); }
public function db_connect() { //connect code goes here }
public function delete($where) { //delete code goes here }
public function insert($data) { //insert code goes here }
public function read($where) { //read code goes here }
public function update($where) { //update code goes here }
} ?>
MSSQLServerDriver.php
<?php
class MSSQLServerDriver extends
DBCommonMethods implements DBInterface { public function __construct($host, $db, $uid, $p
assword)
{
parent::__construct($host, $db, $uid, $password); }
public function db_connect() { //connect code goes here }
public function delete($where) { //delete code goes here }
7
KAZI A S M-9765645688