PHP Classes and OOPS Concept

  • Published on
    10-Aug-2014

  • View
    37.794

  • Download
    1

Embed Size (px)

DESCRIPTION

To find how to use OOPS Concept in PHP and the Class utilization in PHP latest version

Transcript

<ul><li> PHP Classes and Object Orientation </li><li>Revision HIstory # Version Date Rationale for change Change Description 1 1.0 26-Feb-2008 Initial Version 2 </li><li>Revision HIstory Document Name Training Material Document Code QMS-TEM-OT 08 Version 1.0 Date 26-Feb-2008 Created By Ms. Padmavathy Reviewed BY SPG Approved By Mr. Vijay </li><li>Agenda <ul><li>Introduction </li></ul><ul><li>Function </li></ul><ul><li>Class Definition </li></ul><ul><li>Class Usage </li></ul><ul><li>Constructor </li></ul><ul><li>Inheritance </li></ul><ul><li>PHP4 vs PHP5 </li></ul></li><li>Reminder a function <ul><li>Reusable piece of code. </li></ul><ul><li>Has its own local scope. </li></ul><ul><li>function my_func($arg1,$arg2) { </li></ul><ul><li>&gt; </li></ul><ul><li>} </li></ul></li><li>Conceptually, what does a function represent? give the function something (arguments), it does something with them, and then returns a result Action or Method </li><li>What is a class ? Conceptually, a class represents an object , with associated methods and variables </li><li>Class Definition An example class definition for a dog. The dog object has a single attribute, the name, and can perform the action of barking. </li><li>Class Definition class dog { Define the name of the class. </li><li>Class Definition public $name; Define an object attribute (variable), the dogs name. </li><li>Class Definition public function bark() { echo Woof! ; } Define an object action (function), the dogs bark. </li><li>Class Defintion Similar to defining a function.. The definition does not do anything by itself . It is a blueprint, or description, of an object. To do something, you need to use the class </li><li>Class Usage </li><li>Class Usage require ( dog.class.php ); Include the class definition </li><li>Class Usage $puppy = new dog(); Create a new instance of the class. </li><li>Class Usage $puppy-&gt;name = Rover ; Set the name variable of this instance to Rover. </li><li>Class Usage echo {$puppy-&gt;name} says ; Use the name variable of this instance in an echo statement.. </li><li>Class Usage $puppy-&gt;bark(); Use the dog object bark method. </li><li>One dollar and one only $puppy-&gt;name = Rover ; The most common mistake is to use more than one dollar sign when accessing variables. The following means something entirely different.. $puppy-&gt;$name = Rover ; </li><li>Using attributes within the class.. <ul><li>If you need to use the class variables within any class actions, use the special variable $this in the definition: </li></ul><ul><li>class dog { </li></ul><ul><li> public $name; </li></ul><ul><li> public function bark() { </li></ul><ul><li> echo $this-&gt;name. says Woof! ; } </li></ul><ul><li>} </li></ul></li><li>Constructor methods <ul><li>A constructor method is a function that is automatically executed when the class is first instantiated. </li></ul><ul><li>Create a constructor by including a function within the class definition with the __construct name . </li></ul><ul><li>Remember.. if the constructor requires arguments, they must be passed when it is instantiated! </li></ul></li><li>Constructor Example </li><li>Constructor Example Constructor arguments are passed during the instantiation of the object. </li><li>Class Scope <ul><li>Like functions, each instantiated object has its own local scope. </li></ul><ul><li>e.g. if 2 different dog objects are instantiated, $puppy1 and $puppy2 , the two dog names $puppy1-&gt;name and $puppy2-&gt;name are entirely independent.. </li></ul></li><li>Inheritance <ul><li>The real power of using classes is the property of inheritance creating a hierarchy of interlinked classes. </li></ul>dog poodle alsatian parent children </li><li>Inheritance <ul><li>The child classes inherit all the methods and variables of the parent class, and can add extra ones of their own. </li></ul><ul><li>e.g. the child classes poodle inherits the variable name and method bark from the dog class, and can add extra ones </li></ul></li><li>Inheritance example The American Kennel Club (AKC) recognizes three sizes of poodle - Standard, Miniature, and Toy class poodle extends dog { public $type; public function set_type($height) { if ($heighttype = Toy ; } elseif ($height&gt;15) { $this-&gt;type = Standard ; } else { $this-&gt;type = Miniature ; } } } </li><li>Inheritance example The American Kennel Club (AKC) recognizes three sizes of poodle - Standard, Miniature, and Toy class poodle extends dog { public $type; public function set_type($height) { if ($heighttype = Toy ; } elseif ($height&gt;15) { $this-&gt;type = Standard ; } else { $this-&gt;type = Miniature ; } } } class poodle extends dog { Note the use of the extends keyword to indicate that the poodle class is a child of the dog class </li><li>Inheritance example $puppy = new poodle( Oscar ); $puppy-&gt;set_type(12); // 12 inches high! echo Poodle is called {$puppy-&gt;name} , ; echo of type {$puppy-&gt;type} , saying ; echo $puppy-&gt;bark(); </li><li> a poodle will always Yip! <ul><li>It is possible to over-ride a parent method with a new method if it is given the same name in the child class.. </li></ul><ul><li>class poodle extends dog { </li></ul><ul><li>public function bark() { </li></ul><ul><li>echo Yip!; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul></li><li>Child Constructors? <ul><li>If the child class possesses a constructor function, it is executed and any parent constructor is ignored . </li></ul><ul><li>If the child class does not have a constructor, the parents constructor is executed. </li></ul><ul><li>If the child and parent does not have a constructor, the grandparent constructor is attempted </li></ul><ul><li> etc. </li></ul></li><li>Deleting objects <ul><li>So far our objects have not been destroyed till the end of our scripts.. </li></ul><ul><li>Like variables, it is possible to explicitly destroy an object using the unset () function. </li></ul></li><li>There is a lot more <ul><li>We have really only touched the edge of object orientated programming </li></ul><ul><li>But I dont want to confuse you too much! </li></ul></li><li>PHP4 vs. PHP5 <ul><li>OOP purists will tell you that the object support in PHP4 is sketchy. They are right, in that a lot of features are missing. </li></ul><ul><li>PHP5 OOP system has had a big redesign and is much better. </li></ul><ul><li> but it is worth it to produce OOP </li></ul><ul><li>code in either PHP4 or PHP5 </li></ul></li><li>Thank you </li></ul>