Object Oreinted PHP
Object Oreinted PHP
Programming with
PHP
A Presentation by
ADMEC Multimedia Institute & Web Development Institute (WDI) Delhi
Twitter: @admecinstitute
www.admecindia.co.in
www.web-development-institute.c
om
PHP Presentation’s Purpose
The purpose of this PHP presentation is to explain
Object Oriented PHP in brief.
www.admecindia.co.in
www.web-development-institute.com
Topics to be Covered
Basics of OOP in PHP
Object & Classes in OOP
Magic Function in PHP OOP
Inheritance (Extending a class)
Visibility (public, private, protected)
Abstract Classes in PHP
Static Methods and properties in PHP
Interfaces in PHP
Explaining PHP Class Functions
Describing Autoload in PHP
www.admecindia.co.in
www.web-development-institute.com
Concept and Basics of PHP
PHP is a scripting language
Specially a Procedural Programming
Not a true OO Programming
It supports few OO features
Full OOP Support added as of PHP5
What is OOP
It is Object Oriented Programming method of
coding.
Allow developer to group multiple similar
tasks into classes.
The main aim of OOP is to not repeat the
similar code again and again.
With OOP, it is easy to maintain the structure
of application and organizing the data
OOP provide simplicity to the complex
applications code.
www.admecindia.co.in
www.web-development-institute.com
Why is OOP Useful in PHP?
Code organization and maintainability
Add clarity
Reduce complexity
Simple rules allow complex interactions
Emphasizes data over procedure
Code modularity or refactoring
Code reusability
Well-suited for databases
Do You Need OOP?
It depends
My PHP OOP rule of thumb
◦ For a simple site, OOP adds unnecessary complexity
◦ For a complex site, OOP adds necessary simplicity
Classes & Objects in PHP
Concept of classes & objects can be
explained with these two images:
www.admecindia.co.in
www.web-development-institute.com
Classes & Objects in PHP
In PHP class is created using class keyword as shown in the
following example
class myOwnClass
{
//variables of the class
var $variable1;
var $variable2;
//Function of class
function mergeVariable()
{
return $this->variable1 . $this->variable2;
}
}
www.admecindia.co.in
www.web-development-institute.com
Classes & Objects in PHP
There is no use of classes without objects. Object is representative
of your class. If created a class then you must need to create object
of the class to solve your problem using class. You can create object
of your class by using new keyword.
www.admecindia.co.in
www.web-development-institute.com
Magic Functions in PHP OOP
Magic methods in php are some predefined function by php
compiler which executes on some event. Magic methods starts
with prefix __, for example __call, __get, __set.
www.admecindia.co.in
www.web-development-institute.com
Magic Functions in PHP OOP
www.admecindia.co.in
www.web-development-institute.com
Magic Functions in PHP OOP
www.admecindia.co.in
www.web-development-institute.com
Inheritance in PHP
Inheritance is a well-established programming principle, and
PHP makes use of this principle in its object model. This
principle will affect the way many classes and objects relate to
one another.
For example, when you extend a class, the subclass inherits
all of the public and protected methods from the parent class.
Unless a class overrides those methods, they will retain their
original functionality.
This is useful for defining and abstracting functionality, and
permits the implementation of additional functionality in
similar objects without the need to reimplement all of the
shared functionality.
www.admecindia.co.in
www.web-development-institute.com
Visibility in PHP
There are 3 type of visibility available in PHP for controlling
your property or method.
www.admecindia.co.in
www.web-development-institute.com
Abstract Classes in PHP
The abstract classes and methods are used to create a
model of minimum required methods which must be defined
in normal sub-classes derived from an abstract class (with
extends).
www.admecindia.co.in
www.web-development-institute.com
Abstract Classes in PHP
In this example is created an abstract class with a property
($name), an abstract method ( greetName() ) and a normal
method ( setName() ).
<?php
// AbstractClass class
abstract class AbstractClass {
protected $name;
www.admecindia.co.in
www.web-development-institute.com
Static Methods & Properties in
PHP
Static methods and properties in php is very useful feature.
Static methods and properties in php can directly
accessible without creating object of class.
Your php class will be static class if your all methods and
properties of the class is static.
Static Methods and Properties in PHP will be treated as
public if no visibility is defined.
Static properties of class is a property which is directly
accessible from class with the help of ::(scope resolution
operator). You can declare static property using static
keyword.
www.admecindia.co.in
www.web-development-institute.com
Interfaces in PHP
You can define a common structure for your classes using
interfaces.
An Interface is like a template similar to abstract class
with a difference where it uses only abstract methods.
In simple words, an interface is like a class using
interface keyword and contains only function
declarations(function with no body).
An Interface should be implemented in the class and all the
methods or functions should be overridden in this class.
It is the place where we can define the function.
When a class use that interface in that class the total
function body part will describe.
after that we can call that function in other class and we
are get result.
www.admecindia.co.in
www.web-development-institute.com
Interfaces in PHP
Example of interface:
interface InterfaceName{
function fun1();
function fun2(a,b);
}
www.admecindia.co.in
www.web-development-institute.com
Differences between an
Abstract Class and an Interface
inForPHP
abstract class a method must be For interface all the methods by default are
declared as abstract. Abstract methods abstract methods only. So one cannot
doesn’t have any implementation. declare variables or concrete methods in
interfaces.
Abstract class can contain variables Interfaces cannot contain variables and
and concrete methods. concrete methods except constants.
A class can Inherit only one Abstract A class can implement many interfaces
class and Multiple inheritance is not and Multiple interface inheritance is
possible for Abstract class. possible.
www.admecindia.co.in
www.web-development-institute.com
PHP Class Functions in PHP
PHP has available several class functions to help you through the
OOP mine field.
get_declared_interfaces()
class_exists()
get_class()
get_declared_classes()
Each of these is shown here beginning with the
get_declared_interfaces().
www.admecindia.co.in
www.web-development-institute.com
PHP Class Functions in PHP
get_declared_interfaces()
www.admecindia.co.in
www.web-development-institute.com
PHP Class Functions in PHP
public function printBlack(){ }
public function printColor(){ }
public function printDraft(){ }
public function kick(){ }
www.admecindia.co.in
www.web-development-institute.com
Other Functions in PHP
get_class()
class_exists()
get_declared_classes
<?php
/*** a pretend class ***/
class fax{
public function dial(){ }
public function send(){ }
public function recieve(){ }
}
www.admecindia.co.in
www.web-development-institute.com
Other Functions in PHP
/*** create an instance of the fax class ***/
$foo = new fax;
www.admecindia.co.in
www.web-development-institute.com
Autoload in PHP
Many developers writing object-oriented applications create
one PHP source file per class definition. One of the biggest
annoyances is having to write a long list of needed includes at
the beginning of each script (one for each class).
www.admecindia.co.in
www.web-development-institute.com
Autoload in PHP
<?php
/*** include our class definitions ***/
include('classes/vehicle.class.php');
include('classes/motorcycle.class.php');
include('classes/printer.class.php');
include('classes/printer.class.php');
/*** instantiate a new vehicle class object ***/
$vehicle = new vehicle;
*** instantiate a new motorcycle class object ***/
$bike = new motorcycle;
*** instantiate a new printer class object ***/
$printer = new printer;
*** instantiate a new fax class object ***/
$fax = new fax;
?>
www.admecindia.co.in
www.web-development-institute.com
Autoload in PHP
With __autoload() the above code can be reduce to following
<?php
/*** Autoload class files ***/
function __autoload($class){
require('classes/' . strtolower($class) . '.class.php');
}
/*** instantiate a new vehicle class object ***/
$vehicle = new vehicle;
/*** instantiate a new motorcycle class object ***/
$bike = new motorcycle;
/*** instantiate a new printer class object ***/
$printer = new printer;
/*** instantiate a new fax class object ***/
$fax = new fax;
?>
www.admecindia.co.in
www.web-development-institute.com
ADMEC Multimedia Institute
For more info you can visit
www.admecindia.co.in and www.web-development-institute.com
For course related enquiry, ring us at:
9811-81-81-22, 011-3203-5055
www.admecindia.co.in
www.web-development-institute.com