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

PHP Classes and OOPS Concept

  • 1.
    PHPClasses and Object Orientation
  • 2.
    Revision HIstory #Version Date Rationale for change Change Description 1 1.0 26-Feb-2008 Initial Version 2
  • 3.
    Revision HIstory DocumentName 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
  • 4.
    Agenda Introduction FunctionClass Definition Class Usage Constructor Inheritance PHP4 vs PHP5
  • 5.
    Reminder… a functionReusable piece of code. Has its own ‘local scope’. function my_func($arg1,$arg2) { << function statements >> }
  • 6.
    Conceptually, what doesa function represent? … give the function something (arguments), it does something with them, and then returns a result… Action or Method
  • 7.
    What is a class ? Conceptually, a class represents an object , with associated methods and variables
  • 8.
    Class Definition <?phpclass dog { public $name; public function bark() { echo ‘Woof!’ ; } } ?> An example class definition for a dog. The dog object has a single attribute, the name, and can perform the action of barking.
  • 9.
    Class Definition <?phpclass dog { public $name; public function bark() { echo ‘Woof!’ ; } } ?> class dog { Define the name of the class.
  • 10.
    Class Definition <?phpclass dog { public $name; public function bark() { echo ‘Woof!’ ; } } ?> public $name; Define an object attribute (variable), the dog’s name.
  • 11.
    Class Definition <?phpclass dog { public $name; public function bark() { echo ‘Woof!’ ; } } ?> public function bark() { echo ‘Woof!’ ; } Define an object action (function), the dog’s bark.
  • 12.
    Class Defintion Similarto 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…
  • 13.
    Class Usage <?phprequire ( ‘dog.class.php’ ); $puppy = new dog(); $puppy->name = ‘Rover’ ; echo “ {$puppy->name} says ” ; $puppy->bark(); ?>
  • 14.
    Class Usage <?phprequire ( ‘dog.class.php’ ); $puppy = new dog(); $puppy->name = ‘Rover’ ; echo “ {$puppy->name} says ” ; $puppy->bark(); ?> require ( ‘dog.class.php’ ); Include the class definition
  • 15.
    Class Usage <?phprequire ( ‘dog.class.php’ ); $puppy = new dog(); $puppy->name = ‘Rover’ ; echo “ {$puppy->name} says ” ; $puppy->bark(); ?> $puppy = new dog(); Create a new instance of the class.
  • 16.
    Class Usage <?phprequire ( ‘dog.class.php’ ); $puppy = new dog(); $puppy->name = ‘Rover’ ; echo “ {$puppy->name} says ” ; $puppy->bark(); ?> $puppy->name = ‘Rover’ ; Set the name variable of this instance to ‘Rover’.
  • 17.
    Class Usage <?phprequire ( ‘dog.class.php’ ); $puppy = new dog(); $puppy->name = ‘Rover’ ; echo “ {$puppy->name} says ” ; $puppy->bark(); ?> echo “ {$puppy->name} says ” ; Use the name variable of this instance in an echo statement..
  • 18.
    Class Usage <?phprequire ( ‘dog.class.php’ ); $puppy = new dog(); $puppy->name = ‘Rover’ ; echo “ {$puppy->name} says ” ; $puppy->bark(); ?> $puppy->bark(); Use the dog object bark method.
  • 19.
    One dollar andone only… $puppy->name = ‘Rover’ ; The most common mistake is to use more than one dollar sign when accessing variables. The following means something entirely different.. $puppy->$name = ‘Rover’ ;
  • 20.
    Using attributes withinthe class.. If you need to use the class variables within any class actions, use the special variable $this in the definition: class dog { public $name; public function bark() { echo $this->name. ‘ says Woof!’ ; } }
  • 21.
    Constructor methods A constructor method is a function that is automatically executed when the class is first instantiated. Create a constructor by including a function within the class definition with the __construct name . Remember.. if the constructor requires arguments, they must be passed when it is instantiated!
  • 22.
    Constructor Example <?phpclass dog { public $name; public function __construct ($nametext) { $this->name = $nametext; } public function bark() { echo ‘Woof!’; } } ?>
  • 23.
    Constructor Example <?php… $puppy = new dog( ‘Rover’ ); … ?> Constructor arguments are passed during the instantiation of the object.
  • 24.
    Class Scope Likefunctions, each instantiated object has its own local scope. e.g. if 2 different dog objects are instantiated, $puppy1 and $puppy2 , the two dog names $puppy1->name and $puppy2->name are entirely independent..
  • 25.
    Inheritance The realpower of using classes is the property of inheritance – creating a hierarchy of interlinked classes. dog poodle alsatian parent children
  • 26.
    Inheritance The childclasses ‘inherit’ all the methods and variables of the parent class, and can add extra ones of their own. e.g. the child classes poodle inherits the variable ‘name’ and method ‘bark’ from the dog class, and can add extra ones…
  • 27.
    Inheritance example TheAmerican Kennel Club (AKC) recognizes three sizes of poodle -  Standard, Miniature, and Toy … class poodle extends dog { public $type; public function set_type($height) { if ($height<10) { $this->type = ‘Toy’ ; } elseif ($height>15) { $this->type = ‘Standard’ ; } else { $this->type = ‘Miniature’ ; } } }
  • 28.
    Inheritance example TheAmerican Kennel Club (AKC) recognizes three sizes of poodle -  Standard, Miniature, and Toy… class poodle extends dog { public $type; public function set_type($height) { if ($height<10) { $this->type = ‘Toy’ ; } elseif ($height>15) { $this->type = ‘Standard’ ; } else { $this->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…
  • 29.
    Inheritance example …$puppy = new poodle( ‘Oscar’ ); $puppy->set_type(12); // 12 inches high! echo “Poodle is called {$puppy->name} , ” ; echo “of type {$puppy->type} , saying “ ; echo $puppy->bark(); …
  • 30.
    … a poodlewill always ‘Yip!’ It is possible to over-ride a parent method with a new method if it is given the same name in the child class.. class poodle extends dog { … public function bark() { echo ‘Yip!’; } … }
  • 31.
    Child Constructors? Ifthe child class possesses a constructor function, it is executed and any parent constructor is ignored . If the child class does not have a constructor, the parent’s constructor is executed. If the child and parent does not have a constructor, the grandparent constructor is attempted… … etc.
  • 32.
    Deleting objects Sofar our objects have not been destroyed till the end of our scripts.. Like variables, it is possible to explicitly destroy an object using the unset () function.
  • 33.
    There is alot more… We have really only touched the edge of object orientated programming… But I don’t want to confuse you too much!
  • 34.
    PHP4 vs. PHP5OOP purists will tell you that the object support in PHP4 is sketchy. They are right, in that a lot of features are missing. PHP5 OOP system has had a big redesign and is much better. … but it is worth it to produce OOP code in either PHP4 or PHP5…
  • 35.