Object Oreinted PHP | OOPs - PowerPoint PPT Presentation

About This Presentation
Title:

Object Oreinted PHP | OOPs

Description:

This presentation will gives you comprehensive description about object oriented php – PowerPoint PPT presentation

Number of Views:563

less

Transcript and Presenter's Notes

Title: Object Oreinted PHP | OOPs


1
Object Oriented Programming with PHP
  • A Presentation byADMEC Multimedia Institute
    Web Development Institute (WDI) Delhi

Twitter _at_admecinstitute
www.admecindia.co.in www.web-development-institute
.com
2
PHP Presentations Purpose
  • The purpose of this PHP presentation is to
    explain Object Oriented PHP in brief.
  • This PHP presentation can help a PHP programmer
    in understanding the terms and terminologies used
    in practical object oriented PHP.
  • Frameworks of PHP like CodeIgniter, CakePHP,
    Symphony, Zend etc are MVC based and knowledge of
    Object Oriented PHP will help in understanding
    all them.

www.admecindia.co.in www.web-development-institute
.com
3
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
4
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

5
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
6
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

7
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

8
Classes Objects in PHP
  • Concept of classes objects can be explained
    with these two images
  • A class, for example, is like a blueprint for a
    house. It defines the shape of the house on
    paper, with relationships between the different
    parts of the house clearly defined and planned
    out, even though the house doesn't exist.
  • An object, then, is like the actual house built
    according to that blueprint. The data stored in
    the object is like the wood, wires, and concrete
    that compose the house without being assembled
    according to the blueprint, it's just a pile of
    stuff. However, when it all comes together, it
    becomes an organized, useful house.

www.admecindia.co.in www.web-development-institute
.com
9
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 mergeVaria
    ble() return this-gtvariable1 .
    this-gtvariable2

www.admecindia.co.in www.web-development-institute
.com
10
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.
  • objClass new myClass()
  • Now in above code you are creating object of
    class myClass in variable objClass. You can
    create multiple object of your same class. Every
    object is different from other.
  • objClass1 new myClass()objClass2 new
    myClass()

www.admecindia.co.in www.web-development-institute
.com
11
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.
  • List of Magic Functions in PHP

__construct This magic methods is called when someone create object of your class. Usually this is used for creating constructor in php5.
__destruct This magic method is called when object of your class is unset. This is just opposite of __construct.
www.admecindia.co.in www.web-development-institute
.com
12
Magic Functions in PHP OOP
__get This method called when your object attempt to read property or variable of the class which is inaccessible or unavailable.
__set This method called when object of your class attempts to set value of the property which is really inaccessible or unavailable in your class.
__isset This magic methods trigger when isset() function is applied on any property of the class which is inaccessible or unavailable.
__unset __unset is something opposite of isset method. This method triggers when unset() function called on inaccessible or unavailable property of the class.
www.admecindia.co.in www.web-development-institute
.com
13
Magic Functions in PHP OOP
__call __call magic method trigger when you are attempting to call method or function of the class which is either inaccessible or unavailable.
__callstatic __callstatic execture when inaccessible or unavailable method is in static context.
__sleep __sleep methods trigger when you are going to serialize your class object.
__wakeup __wakeup executes when you are un serializing any class object.
www.admecindia.co.in www.web-development-institute
.com
14
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
15
Visibility in PHP
  • There are 3 type of visibility available in PHP
    for controlling your property or method.
  • Public Public method or variable can be
    accessible from anywhere. I mean from inside the
    class, out side the class and in child(will
    discuss in next chapter) class also.
  • Private Method or property with private
    visibility can only be accessible inside the
    class. You can not access private method or
    variable from outside of your class.
  • Protected Method or variable with protected
    visibility can only be access in the derived
    class. Or in other word in child class. Protected
    will be used in the process of inheritance.

www.admecindia.co.in www.web-development-institute
.com
16
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).
  • An abstract class is created with the abstract
    keyword.
  • An abstract class cannot be instantiated, can
    only be inherited by other sub-classes extended
    from it.
  • The abstract methods are declared with the
    abstract keyword, and cannot have an
    implementation, they simply declare the method's
    signature. abstract public function method
    Name(arguments)

www.admecindia.co.in www.web-development-institute
.com
17
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() ).
  • lt?php
  • // AbstractClass class
  • abstract class AbstractClass
  • protected name
  • // declare an abstract method
  • abstract public function greetName(greet)
  • // define a normal method
  • public function setName(name)
  • this-gtname name // sets the value of
    name property
  • ?gt

www.admecindia.co.in www.web-development-institute
.com
18
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
19
Interfaces in PHP
  • You can define a common structure for your
    classes using interfaces.
  • An Interface is like a template similar to
    abstract classwith a difference where it uses
    only abstract methods.
  • In simple words, an interface is like a class
    usinginterface keyword and contains only
    functiondeclarations(function with no body).
  • An Interface should be implemented in the class
    and all themethods 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
20
Interfaces in PHP
  • Example of interface
  • interface InterfaceName function fun1()
    function fun2(a,b) class ClassName
    implements InterfaceName fuction fun1()
    function implementation.......
    fuction fun2(a,b) function
    implementation.......  

www.admecindia.co.in www.web-development-institute
.com
21
Differences between an Abstract Class and an
Interface in PHP
  • For abstract class a method must be declared as
    abstract. Abstract methods doesnt have any
    implementation.
  • The Abstract methods can declare with Access
    modifiers like public, internal, protected. When
    implementing in subclass these methods must be
    defined with the same (or a less restricted)
    visibility.
  • Abstract class can contain variables and concrete
    methods.
  • A class can Inherit only one Abstract class and
    Multiple inheritance is not possible for Abstract
    class.

For interface all the methods by default are
abstract methods only. So one cannot declare
variables or concrete methods in
interfaces. All methods declared in an
interface must be public. Interfaces cannot
contain variables and concrete methods except
constants. A class can implement many
interfaces and Multiple interface inheritance is
possible.
www.admecindia.co.in www.web-development-institute
.com
22
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
23
PHP Class Functions in PHP
  • get_declared_interfaces()
  • following function provides an array of all the
    available declared interfaces.
  • lt?phpinterface fax  public function dial()  p
    ublic function send()  public function recieve()
    interface printer  public function printBla
    ck()  public function printColor()  public fun
    ction printDraft()  public function kick()/
     our interface implementation /class printe
    rFax implements fax, printer  public function di
    al()   public function send()   public funct
    ion recieve() 

www.admecindia.co.in www.web-development-institute
.com
24
PHP Class Functions in PHP
  •   public function printBlack()   public functio
    n printColor()   public function printDraft() 
      public function kick() 
  •   / create and printerfax object /  obj
    ect  new printerFax  / get the declared inte
    rfaces /  foreach(get_declared_interfaces() as
     keygtinterface)                echo key.' 
    gt '.interface.'ltbr /gt'        ?gt

www.admecindia.co.in www.web-development-institute
.com
25
Other Functions in PHP
  • get_class()
  • class_exists()
  • get_declared_classes

lt?php/ a pretend class /class fax  publi
c function dial()   public function send()  
 public function recieve() / another pret
end class /class printer  public function pr
intBlack()   public function printColor()   
public function printDraft()   public function 
kick() 
www.admecindia.co.in www.web-development-institute
.com
26
Other Functions in PHP
  • / create an instance of the fax class /fo
    o  new fax/ create an instance of the print
    er class /bar  new printerecho 'foo is f
    rom the ' get_class(foo).' classltbr /gt'echo cla
    ss_exists("printer").'ltbr /gt'echo 'Declared clas
    ses areltbr /gt 'foreach(get_declared_classes() a
    s keygtclassname)        echo key.' -gt '.
    classname.'ltbr /gt'    ?gt

www.admecindia.co.in www.web-development-institute
.com
27
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).
  • In PHP 5, this is no longer necessary. You may
    define an __autoload() function which is
    automatically called in case you are trying to
    use a class/interface which hasn't been defined
    yet. By calling this function the scripting
    engine is given a last chance to load the class
    before PHP fails with an error.

www.admecindia.co.in www.web-development-institute
.com
28
Autoload in PHP
  • lt?php/ include our class definitions /in
    clude('classes/vehicle.class.php')include('clas
    ses/motorcycle.class.php')include('classes/prin
    ter.class.php')include('classes/printer.class.p
    hp')/ instantiate a new vehicle class object
     /vehicle  new vehicle instantiate a n
    ew motorcycle class object /bike  new motorc
    ycle instantiate a new printer class object 
    /printer  new printer instantiate a ne
    w fax class object /fax  new fax?gt

www.admecindia.co.in www.web-development-institute
.com
29
Autoload in PHP
  • With __autoload() the above code can be reduce to
    following
  • lt?php/ Autoload class files /function __a
    utoload(class)  require('classes/' . strtolower
    (class) . '.class.php')/ instantiate a ne
    w vehicle class object /vehicle  new vehicle
    / instantiate a new motorcycle class object 
    /bike  new motorcycle/ instantiate a n
    ew printer class object /printer  new printe
    r/ instantiate a new fax class object /
    fax  new fax?gt

www.admecindia.co.in www.web-development-institute
.com
30
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
Created by Parul Sabal Guided by Ravi Bhadauria
www.admecindia.co.in www.web-development-institute
.com
Write a Comment
User Comments (0)
About PowerShow.com