SlideShare a Scribd company logo
PHP Classes  and  Object Orientation
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 { var  $name public function  bark() { echo   ‘Woof!’ ; } }  ?> public  $name; Define an object attribute (variable), the dog’s name.
Class Definition <?php class  dog { public  $name; function  bark() { echo   ‘Woof!’ ; } }  ?> public function  bark() { echo   ‘Woof!’ ; } Define an object action (function), the dog’s bark.
Class Definition <?php class  dog { public  $name; public function  bark() { echo   ‘Woof!’ ; } }  ?> } End the class definition
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.
Class Usage <?php require ( ‘dog.class.php’ ); $puppy =  new  dog(); $puppy->name =  ‘Rover’ ; echo  “ {$puppy->name}  says ” ; $puppy->bark(); ?> [example file: classes1.php]
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 function
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.
Objects within Objects It is perfectly possible to include objects within another object.. class  dogtag {      public  $words; } class  dog {      public  $name;      public  $tag;      public function  bark() {          echo  &quot;Woof!\n&quot;;     } }  … $puppy =  new  dog; $puppy->name =  “Rover&quot; ; $poppy->tag =  new  dogtag; $poppy->tag->words =  “blah” ; …
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.
A copy, or not a copy.. Entire objects can be passed as arguments to functions, and can use all methods/variables within the function.  Remember however.. like functions the object is  COPIED  when passed as an argument unless you specify the argument as a reference variable  &$variable
Why Object Orientate? Reason 1 Once you have your head round the concept of objects, intuitively named object orientated code becomes  easy to understand . e.g.    $order->display_basket();   $user->card[2]->pay($order);   $order->display_status();
Why Object Orientate? Reason 2 Existing code becomes easier to maintain. e.g. If you want to extend the capability of a piece of code, you can merely edit the class definitions…
Why Object Orientate? Reason 3 New code becomes much quicker to write once you have a suitable class library. e.g. Need a new object..? Usually can extend an existing object. A lot of high quality code is distributed as classes (e.g.  http:// pear.php.net ).
There is a lot more… We have really only touched the edge of object orientated programming… http:// www.php.net/manual/en/language.oop.php   …  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…

More Related Content

PPT
PHP - Introduction to Object Oriented Programming with PHP
Vibrant Technologies & Computers
 
PPSX
Sessions and cookies
www.netgains.org
 
PPTX
Database Connectivity in PHP
Taha Malampatti
 
PPTX
What is an API?
Muhammad Zuhdi
 
PDF
Bootstrap 5 basic
Jubair Ahmed Junjun
 
PDF
2024 Trend Updates: What Really Works In SEO & Content Marketing
Search Engine Journal
 
PPTX
Cascading Style Sheet (CSS)
AakankshaR
 
PHP - Introduction to Object Oriented Programming with PHP
Vibrant Technologies & Computers
 
Sessions and cookies
www.netgains.org
 
Database Connectivity in PHP
Taha Malampatti
 
What is an API?
Muhammad Zuhdi
 
Bootstrap 5 basic
Jubair Ahmed Junjun
 
2024 Trend Updates: What Really Works In SEO & Content Marketing
Search Engine Journal
 
Cascading Style Sheet (CSS)
AakankshaR
 

What's hot (20)

PDF
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
PPTX
jQuery
Dileep Mishra
 
PPTX
Dom(document object model)
Partnered Health
 
PPT
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
PPTX
Lab #2: Introduction to Javascript
Walid Ashraf
 
PPTX
Delegates and events in C#
Dr.Neeraj Kumar Pandey
 
PPTX
Css selectors
Parth Trivedi
 
PPT
CSS for Beginners
Amit Kumar Singh
 
PDF
jQuery for beginners
Arulmurugan Rajaraman
 
ODP
Introduction of Html/css/js
Knoldus Inc.
 
PDF
JavaScript Programming
Sehwan Noh
 
PDF
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
PPT
Php forms
Anne Lee
 
PPT
PHP - Introduction to PHP Forms
Vibrant Technologies & Computers
 
PPTX
concept of oops
prince sharma
 
PDF
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Edureka!
 
PPTX
Form Handling using PHP
Nisa Soomro
 
PDF
JavaScript - Chapter 11 - Events
WebStackAcademy
 
PDF
Php introduction
krishnapriya Tadepalli
 
Python programming : Classes objects
Emertxe Information Technologies Pvt Ltd
 
Dom(document object model)
Partnered Health
 
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
Lab #2: Introduction to Javascript
Walid Ashraf
 
Delegates and events in C#
Dr.Neeraj Kumar Pandey
 
Css selectors
Parth Trivedi
 
CSS for Beginners
Amit Kumar Singh
 
jQuery for beginners
Arulmurugan Rajaraman
 
Introduction of Html/css/js
Knoldus Inc.
 
JavaScript Programming
Sehwan Noh
 
JavaScript - Chapter 12 - Document Object Model
WebStackAcademy
 
Php forms
Anne Lee
 
PHP - Introduction to PHP Forms
Vibrant Technologies & Computers
 
concept of oops
prince sharma
 
Python Flask Tutorial For Beginners | Flask Web Development Tutorial | Python...
Edureka!
 
Form Handling using PHP
Nisa Soomro
 
JavaScript - Chapter 11 - Events
WebStackAcademy
 
Php introduction
krishnapriya Tadepalli
 
Ad

Viewers also liked (20)

PPT
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
ODP
Beginners Guide to Object Orientation in PHP
Rick Ogden
 
PPT
radar
Ramasubbu .P
 
PPTX
Oop in-php
Rajesh S
 
PDF
Modul 3 Cara Membuat View Pada CodeIgniter
Riki Afriansyah
 
PPTX
Modular PHP Development using CodeIgniter Bonfire
Jeff Fox
 
PPT
Functions in php
Mudasir Syed
 
PPT
Guide
Ramasubbu .P
 
PPT
My SQl
Ramasubbu .P
 
PPT
sql
Ramasubbu .P
 
PPT
IP Addressing
Ramasubbu .P
 
PPT
Saftey
Ramasubbu .P
 
PPT
Mysql
Ramasubbu .P
 
PPTX
1. review jurnal effect dwi hastho
Hastho Oke Sekali Jaya
 
PPT
I/O Management
Ramasubbu .P
 
PPT
Shell Script
Ramasubbu .P
 
PPT
Operating systems
Eduardo Triana
 
PPT
Network Security
Ramasubbu .P
 
PDF
The Dining Philosophers problem in Bangla
Sifat Hossain
 
PPT
MSAT
Ramasubbu .P
 
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
Beginners Guide to Object Orientation in PHP
Rick Ogden
 
Oop in-php
Rajesh S
 
Modul 3 Cara Membuat View Pada CodeIgniter
Riki Afriansyah
 
Modular PHP Development using CodeIgniter Bonfire
Jeff Fox
 
Functions in php
Mudasir Syed
 
My SQl
Ramasubbu .P
 
IP Addressing
Ramasubbu .P
 
Saftey
Ramasubbu .P
 
1. review jurnal effect dwi hastho
Hastho Oke Sekali Jaya
 
I/O Management
Ramasubbu .P
 
Shell Script
Ramasubbu .P
 
Operating systems
Eduardo Triana
 
Network Security
Ramasubbu .P
 
The Dining Philosophers problem in Bangla
Sifat Hossain
 
Ad

Similar to Class and Objects in PHP (20)

PPT
Oops implemetation material
Deepak Solanki
 
PPT
SQL Devlopment for 10 ppt
Tanay Kishore Mishra
 
PPTX
06-classes.ppt (copy).pptx
Thắng It
 
PPT
Php Oop
mussawir20
 
PPTX
Ch8(oop)
Chhom Karath
 
PPT
OOP
thinkphp
 
PPTX
Object-Oriented Programming with PHP (part 1)
Bozhidar Boshnakov
 
PDF
Object Oriented Programming in PHP
wahidullah mudaser
 
PPTX
Lecture 17 - PHP-Object-Orientation.pptx
DavidLazar17
 
PPTX
OOP in PHP.pptx
switipatel4
 
PPT
14 Defining classes
maznabili
 
PPTX
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
PPTX
UNIT III (8).pptx
DrDhivyaaCRAssistant
 
PPTX
UNIT III (8).pptx
DrDhivyaaCRAssistant
 
ODP
Intro to The PHP SPL
Chris Tankersley
 
PPTX
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
PPT
Go OO! - Real-life Design Patterns in PHP 5
Stephan Schmidt
 
DOCX
Oops concept in php
selvabalaji k
 
PDF
Demystifying Object-Oriented Programming - Lone Star PHP
Alena Holligan
 
Oops implemetation material
Deepak Solanki
 
SQL Devlopment for 10 ppt
Tanay Kishore Mishra
 
06-classes.ppt (copy).pptx
Thắng It
 
Php Oop
mussawir20
 
Ch8(oop)
Chhom Karath
 
Object-Oriented Programming with PHP (part 1)
Bozhidar Boshnakov
 
Object Oriented Programming in PHP
wahidullah mudaser
 
Lecture 17 - PHP-Object-Orientation.pptx
DavidLazar17
 
OOP in PHP.pptx
switipatel4
 
14 Defining classes
maznabili
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
UNIT III (8).pptx
DrDhivyaaCRAssistant
 
UNIT III (8).pptx
DrDhivyaaCRAssistant
 
Intro to The PHP SPL
Chris Tankersley
 
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
Go OO! - Real-life Design Patterns in PHP 5
Stephan Schmidt
 
Oops concept in php
selvabalaji k
 
Demystifying Object-Oriented Programming - Lone Star PHP
Alena Holligan
 

More from Ramasubbu .P (20)

PPT
Press
Ramasubbu .P
 
PPT
Milling 2
Ramasubbu .P
 
PPT
MIlling 1
Ramasubbu .P
 
PPT
Drillings
Ramasubbu .P
 
PPT
Holding
Ramasubbu .P
 
PPT
Harvesting
Ramasubbu .P
 
PPT
Plough
Ramasubbu .P
 
PPT
Tractor PTO
Ramasubbu .P
 
PPT
Tractor Components
Ramasubbu .P
 
PPT
GPS
Ramasubbu .P
 
PPT
RTOS
Ramasubbu .P
 
PPT
Virus
Ramasubbu .P
 
PPT
Hacker
Ramasubbu .P
 
PPT
Denail of Service
Ramasubbu .P
 
PPT
RAID CONCEPT
Ramasubbu .P
 
PPT
Timer
Ramasubbu .P
 
PPT
Sequential Logic Circuit
Ramasubbu .P
 
PPT
PL C
Ramasubbu .P
 
PPT
P L C
Ramasubbu .P
 
PPT
Ladder
Ramasubbu .P
 
Milling 2
Ramasubbu .P
 
MIlling 1
Ramasubbu .P
 
Drillings
Ramasubbu .P
 
Holding
Ramasubbu .P
 
Harvesting
Ramasubbu .P
 
Plough
Ramasubbu .P
 
Tractor PTO
Ramasubbu .P
 
Tractor Components
Ramasubbu .P
 
Hacker
Ramasubbu .P
 
Denail of Service
Ramasubbu .P
 
RAID CONCEPT
Ramasubbu .P
 
Sequential Logic Circuit
Ramasubbu .P
 
Ladder
Ramasubbu .P
 

Recently uploaded (20)

PDF
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
PDF
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPT
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PPTX
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
PDF
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
PPTX
How to Manage Global Discount in Odoo 18 POS
Celine George
 
PDF
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
DOCX
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
PDF
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
PPTX
Care of patients with elImination deviation.pptx
AneetaSharma15
 
PPTX
Congenital Hypothyroidism pptx
AneetaSharma15
 
DOCX
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
PPTX
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
PDF
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
PPTX
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
PDF
Landforms and landscapes data surprise preview
jpinnuck
 
The Picture of Dorian Gray summary and depiction
opaliyahemel
 
Exploring-Forces 5.pdf/8th science curiosity/by sandeep swamy notes/ppt
Sandeep Swamy
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Python Programming Unit II Control Statements.ppt
CUO VEERANAN VEERANAN
 
PREVENTIVE PEDIATRIC. pptx
AneetaSharma15
 
5.EXPLORING-FORCES-Detailed-Notes.pdf/8TH CLASS SCIENCE CURIOSITY
Sandeep Swamy
 
How to Manage Global Discount in Odoo 18 POS
Celine George
 
PG-BPSDMP 2 TAHUN 2025PG-BPSDMP 2 TAHUN 2025.pdf
AshifaRamadhani
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Action Plan_ARAL PROGRAM_ STAND ALONE SHS.docx
Levenmartlacuna1
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
IMMUNIZATION PROGRAMME pptx
AneetaSharma15
 
Arihant Class 10 All in One Maths full pdf
sajal kumar
 
Care of patients with elImination deviation.pptx
AneetaSharma15
 
Congenital Hypothyroidism pptx
AneetaSharma15
 
SAROCES Action-Plan FOR ARAL PROGRAM IN DEPED
Levenmartlacuna1
 
How to Manage Leads in Odoo 18 CRM - Odoo Slides
Celine George
 
UTS Health Student Promotional Representative_Position Description.pdf
Faculty of Health, University of Technology Sydney
 
Software Engineering BSC DS UNIT 1 .pptx
Dr. Pallawi Bulakh
 
Landforms and landscapes data surprise preview
jpinnuck
 

Class and Objects in PHP

  • 1. PHP Classes and Object Orientation
  • 2. Reminder… a function Reusable piece of code. Has its own ‘local scope’. function my_func($arg1,$arg2) { << function statements >> }
  • 3. Conceptually, what does a function represent? … give the function something (arguments), it does something with them, and then returns a result… Action or Method
  • 4. What is a class ? Conceptually, a class represents an object , with associated methods and variables
  • 5. 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.
  • 6. Class Definition <?php class dog { public $name; public function bark() { echo ‘Woof!’ ; } } ?> class dog { Define the name of the class.
  • 7. Class Definition <?php class dog { var $name public function bark() { echo ‘Woof!’ ; } } ?> public $name; Define an object attribute (variable), the dog’s name.
  • 8. Class Definition <?php class dog { public $name; function bark() { echo ‘Woof!’ ; } } ?> public function bark() { echo ‘Woof!’ ; } Define an object action (function), the dog’s bark.
  • 9. Class Definition <?php class dog { public $name; public function bark() { echo ‘Woof!’ ; } } ?> } End the class definition
  • 10. 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…
  • 11. Class Usage <?php require ( ‘dog.class.php’ ); $puppy = new dog(); $puppy->name = ‘Rover’ ; echo “ {$puppy->name} says ” ; $puppy->bark(); ?>
  • 12. 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
  • 13. 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.
  • 14. 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’.
  • 15. 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..
  • 16. 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.
  • 17. Class Usage <?php require ( ‘dog.class.php’ ); $puppy = new dog(); $puppy->name = ‘Rover’ ; echo “ {$puppy->name} says ” ; $puppy->bark(); ?> [example file: classes1.php]
  • 18. 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’ ;
  • 19. 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!’ ; } }
  • 20. 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!
  • 21. Constructor Example <?php class dog { public $name; public function __construct ($nametext) { $this->name = $nametext; } public function bark() { echo ‘Woof!’; } } ?> Constructor function
  • 22. Constructor Example <?php … $puppy = new dog( ‘Rover’ ); … ?> Constructor arguments are passed during the instantiation of the object.
  • 23. 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..
  • 24. Inheritance The real power of using classes is the property of inheritance – creating a hierarchy of interlinked classes. dog poodle alsatian parent children
  • 25. 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…
  • 26. 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’ ; } } }
  • 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’ ; } } } class poodle extends dog { Note the use of the extends keyword to indicate that the poodle class is a child of the dog class…
  • 28. 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(); …
  • 29. …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!’; } … }
  • 30. 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.
  • 31. Objects within Objects It is perfectly possible to include objects within another object.. class dogtag {      public $words; } class dog {      public $name;      public $tag;      public function bark() {          echo &quot;Woof!\n&quot;;     } } … $puppy = new dog; $puppy->name = “Rover&quot; ; $poppy->tag = new dogtag; $poppy->tag->words = “blah” ; …
  • 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. A copy, or not a copy.. Entire objects can be passed as arguments to functions, and can use all methods/variables within the function. Remember however.. like functions the object is COPIED when passed as an argument unless you specify the argument as a reference variable &$variable
  • 34. Why Object Orientate? Reason 1 Once you have your head round the concept of objects, intuitively named object orientated code becomes easy to understand . e.g. $order->display_basket(); $user->card[2]->pay($order); $order->display_status();
  • 35. Why Object Orientate? Reason 2 Existing code becomes easier to maintain. e.g. If you want to extend the capability of a piece of code, you can merely edit the class definitions…
  • 36. Why Object Orientate? Reason 3 New code becomes much quicker to write once you have a suitable class library. e.g. Need a new object..? Usually can extend an existing object. A lot of high quality code is distributed as classes (e.g. http:// pear.php.net ).
  • 37. There is a lot more… We have really only touched the edge of object orientated programming… http:// www.php.net/manual/en/language.oop.php … but I don’t want to confuse you too much!
  • 38. 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…