SlideShare a Scribd company logo
  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

More Related Content

PPT
Class and Objects in PHP
PPT
Oops in PHP
PDF
Object Oriented Programming with PHP 5 - More OOP
PPT
Php Oop
PPTX
Php oop presentation
PPT
Class 7 - PHP Object Oriented Programming
PPTX
Object oreinted php | OOPs
PPTX
Object oriented programming in php 5
Class and Objects in PHP
Oops in PHP
Object Oriented Programming with PHP 5 - More OOP
Php Oop
Php oop presentation
Class 7 - PHP Object Oriented Programming
Object oreinted php | OOPs
Object oriented programming in php 5

What's hot (20)

PPT
Oops in PHP By Nyros Developer
PPTX
Oop in-php
PPT
Oops concepts in php
PDF
OOP in PHP
PDF
Object Oriented Programming in PHP
PPT
Introduction to OOP with PHP
PPTX
Oops in php
PDF
A Gentle Introduction To Object Oriented Php
ZIP
Object Oriented PHP5
PPT
PHP- Introduction to Object Oriented PHP
PDF
Intermediate OOP in PHP
PPTX
Intro to OOP PHP and Github
PPTX
Introduction to PHP OOP
PDF
09 Object Oriented Programming in PHP #burningkeyboards
PPT
PHP - Introduction to Object Oriented Programming with PHP
PPTX
Basics of Object Oriented Programming in Python
PPTX
Only oop
PDF
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
PPTX
OOPS Characteristics (With Examples in PHP)
Oops in PHP By Nyros Developer
Oop in-php
Oops concepts in php
OOP in PHP
Object Oriented Programming in PHP
Introduction to OOP with PHP
Oops in php
A Gentle Introduction To Object Oriented Php
Object Oriented PHP5
PHP- Introduction to Object Oriented PHP
Intermediate OOP in PHP
Intro to OOP PHP and Github
Introduction to PHP OOP
09 Object Oriented Programming in PHP #burningkeyboards
PHP - Introduction to Object Oriented Programming with PHP
Basics of Object Oriented Programming in Python
Only oop
PYTHON-Chapter 3-Classes and Object-oriented Programming: MAULIK BORSANIYA
OOPS Characteristics (With Examples in PHP)
Ad

Viewers also liked (18)

ODP
Beginners Guide to Object Orientation in PHP
PDF
Login and Registration form using oop in php
PPT
Basic Oops concept of PHP
PPT
General OOP Concepts
PPTX
Advance enginering mathematics
PPTX
Oop concepts
PPT
Oop concepts
PDF
Beginning OOP in PHP
PPTX
BUILDING WEBSITES ON WORDPRESS
PDF
Dropr - The Message Queue project for PHP
PPT
Ejobportal project ppt on php my_sql
PPTX
Lan chatting and file transfer Project on PHP
PDF
Zend Php Certification Study Guide
PDF
PHP based School ERP
PPTX
Web School - School Management System
DOCX
Useful functions for arrays in php
PDF
Php login system with admin features evolt
PPT
General OOP concept [by-Digvijay]
Beginners Guide to Object Orientation in PHP
Login and Registration form using oop in php
Basic Oops concept of PHP
General OOP Concepts
Advance enginering mathematics
Oop concepts
Oop concepts
Beginning OOP in PHP
BUILDING WEBSITES ON WORDPRESS
Dropr - The Message Queue project for PHP
Ejobportal project ppt on php my_sql
Lan chatting and file transfer Project on PHP
Zend Php Certification Study Guide
PHP based School ERP
Web School - School Management System
Useful functions for arrays in php
Php login system with admin features evolt
General OOP concept [by-Digvijay]
Ad

Similar to PHP Classes and OOPS Concept (20)

PPT
10 classes
PPT
Oops implemetation material
PPTX
06-classes.ppt (copy).pptx
PPT
SQL Devlopment for 10 ppt
PPT
PPTX
Ch8(oop)
PDF
Objects, Testing, and Responsibility
PDF
Object Oriented Programming in PHP
PPTX
Object-Oriented Programming with PHP (part 1)
PPT
Go OO! - Real-life Design Patterns in PHP 5
PPTX
PPTX
PHP OOP Lecture - 04.pptx
PPTX
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
PPTX
UNIT III (8).pptx
PPTX
UNIT III (8).pptx
PDF
Demystifying Object-Oriented Programming - Lone Star PHP
PDF
OOP is more than Cars and Dogs
PDF
SPL: The Missing Link in Development
DOCX
Oops concept in php
PPT
14 Defining classes
10 classes
Oops implemetation material
06-classes.ppt (copy).pptx
SQL Devlopment for 10 ppt
Ch8(oop)
Objects, Testing, and Responsibility
Object Oriented Programming in PHP
Object-Oriented Programming with PHP (part 1)
Go OO! - Real-life Design Patterns in PHP 5
PHP OOP Lecture - 04.pptx
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
UNIT III (8).pptx
UNIT III (8).pptx
Demystifying Object-Oriented Programming - Lone Star PHP
OOP is more than Cars and Dogs
SPL: The Missing Link in Development
Oops concept in php
14 Defining classes

More from Dot Com Infoway - Custom Software, Mobile, Web Application Development and Digital Marketing Company (20)

PDF
PDF
The Future of AI in Development: Opportunities, Challenges, and Trends for 2025
PDF
Thinking of Switching App Developers or Agencies? 8 Essentials You Must Know ...
PDF
The Ultimate Guide_ Boost Your E-commerce Sales by 10x with Google’s Performa...
PDF
How to turn the challenge of covid 19 into a digitalzation opportunity
PPTX
The Future of AI in Development: Opportunities, Challenges, and Trends for 2025
Thinking of Switching App Developers or Agencies? 8 Essentials You Must Know ...
The Ultimate Guide_ Boost Your E-commerce Sales by 10x with Google’s Performa...
How to turn the challenge of covid 19 into a digitalzation opportunity

Recently uploaded (20)

PDF
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
PDF
GamePlan Trading System Review: Professional Trader's Honest Take
PPTX
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
PDF
Transforming Manufacturing operations through Intelligent Integrations
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Advanced Soft Computing BINUS July 2025.pdf
PDF
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
cuic standard and advanced reporting.pdf
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Modernizing your data center with Dell and AMD
PPTX
Cloud computing and distributed systems.
PPT
Teaching material agriculture food technology
PDF
Empathic Computing: Creating Shared Understanding
PDF
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...
solutions_manual_-_materials___processing_in_manufacturing__demargo_.pdf
GamePlan Trading System Review: Professional Trader's Honest Take
Telecom Fraud Prevention Guide | Hyperlink InfoSystem
Transforming Manufacturing operations through Intelligent Integrations
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
NewMind AI Weekly Chronicles - August'25 Week I
How Onsite IT Support Drives Business Efficiency, Security, and Growth.pdf
Per capita expenditure prediction using model stacking based on satellite ima...
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Advanced Soft Computing BINUS July 2025.pdf
HCSP-Presales-Campus Network Planning and Design V1.0 Training Material-Witho...
Chapter 3 Spatial Domain Image Processing.pdf
cuic standard and advanced reporting.pdf
Understanding_Digital_Forensics_Presentation.pptx
Modernizing your data center with Dell and AMD
Cloud computing and distributed systems.
Teaching material agriculture food technology
Empathic Computing: Creating Shared Understanding
GDG Cloud Iasi [PUBLIC] Florian Blaga - Unveiling the Evolution of Cybersecur...

PHP Classes and OOPS Concept

  • 1. PHP Classes 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 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
  • 4. Agenda Introduction Function Class Definition Class Usage Constructor Inheritance PHP4 vs PHP5
  • 5. Reminder… a function Reusable piece of code. Has its own ‘local scope’. function my_func($arg1,$arg2) { << function statements >> }
  • 6. Conceptually, what does a 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 <?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.
  • 9. Class Definition <?php class dog { public $name; public function bark() { echo ‘Woof!’ ; } } ?> class dog { Define the name of the class.
  • 10. Class Definition <?php class dog { public $name; public function bark() { echo ‘Woof!’ ; } } ?> public $name; Define an object attribute (variable), the dog’s name.
  • 11. 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.
  • 12. 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…
  • 13. Class Usage <?php require ( ‘dog.class.php’ ); $puppy = new dog(); $puppy->name = ‘Rover’ ; echo “ {$puppy->name} says ” ; $puppy->bark(); ?>
  • 14. 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
  • 15. 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.
  • 16. 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’.
  • 17. 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..
  • 18. 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.
  • 19. 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’ ;
  • 20. 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!’ ; } }
  • 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 <?php class 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 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..
  • 25. Inheritance The real power of using classes is the property of inheritance – creating a hierarchy of interlinked classes. dog poodle alsatian parent children
  • 26. 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…
  • 27. 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’ ; } } }
  • 28. 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…
  • 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 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!’; } … }
  • 31. 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.
  • 32. 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.
  • 33. 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!
  • 34. 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…