PHP
What is PHP? Write your first PHP Program
What is PHP? PHP is a server side scripting language. that is used to develop Static websites or...
The three basic components of object orientation are;
The three major principles of OOP are;
PHP is an object oriented scripting language; it supports all of the above principles. The above principles are achieved via;
Now that we have the basic knowledge of OOP and how it is supported in PHP, let us look at examples that implement the above principles
Unified Modeling Language UML is a technique used to design and document object oriented systems.
UML produces a number of documents, but we will look at the class diagram which is very important to object oriented php programming.
Class Diagram Example
Class Diagram Key
The class keyword is used to define a class in PHP. Below are the rules for creating a class in PHP.
Let’s say we want to create a class for representing animals.
We will start with identifying the features that are common to all animals.
The diagram below shows the diagram for the animal
Let’s now code our animal class
<?php
class Animal
{
private $family;
private $food;
public function __construct($family, $food)
{
$this->family = $family;
$this->food = $food;
}
public function get_family()
{
return $this->family;
}
public function set_family($family)
{
$this->family = $family;
}
public function get_food()
{
return $this->food;
}
public function set_food($food)
{
$this->food = $food;
}
}
?> HERE,
We will work with a cow and a lion. Both the cow and lion inherit from the Animal class.
The class diagram below shows the relationships.
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)
{
parent::__construct($family, $food);
}
}
?>HERE,
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() . '<br><br>';
echo '<b>Lion Object</b> <br>';
echo 'The Lion belongs to the ' . $lion->get_family() . ' family and eats ' . $lion->get_food();
?>
Let’s now view our application in a web browser
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.
The class diagram below illustrates the relationship among our abstract class, interface, and implementation classes.
Let’s now create our abstract class
<?php
abstract class DBCommonMethods
{
private $host;
private $db;
private $uid;
private $password;
public function __construct($host, $db, $uid, $password)
{
$this->host = $host;
$this->db = $db;
$this->uid = $uid;
$this->password = $password;
}
}
?>HERE,
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,
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, $password)
{
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, $password)
{
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 }
} ?>HERE,
Usage of the above code The code using the above class would look like this
<?php $db = new MySQLDriver($host,$db,$uid,$password); ?>
Or
<?php $db = new MSSQLServerDriver ($host,$db,$uid,$password); ?>
The rest of the code would be the same for both drivers such as;
<?php $db->db_connect(); $db->insert($data); ?>
What is PHP? PHP is a server side scripting language. that is used to develop Static websites or...
Why use Comments? If you don’t work on the source code for some time, it’s easy to forget what the code...
What is Regular expression in PHP? PHP Regular Expression also known as regex are powerful pattern...
What is PHP Date Function? PHP date function is an in-built function that simplify working with...
PHP is a server-side scripting language used to develop static and dynamic websites or web...
What is Cookie? A cookie is a small file with the maximum size of 4KB that the web server stores...