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…
Ad

More Related Content

What's hot (20)

PHP - Introduction to File Handling with PHP
PHP -  Introduction to  File Handling with PHPPHP -  Introduction to  File Handling with PHP
PHP - Introduction to File Handling with PHP
Vibrant Technologies & Computers
 
Php string function
Php string function Php string function
Php string function
Ravi Bhadauria
 
Complete Lecture on Css presentation
Complete Lecture on Css presentation Complete Lecture on Css presentation
Complete Lecture on Css presentation
Salman Memon
 
CSS Basics
CSS BasicsCSS Basics
CSS Basics
WordPress Memphis
 
Mysql Crud, Php Mysql, php, sql
Mysql Crud, Php Mysql, php, sqlMysql Crud, Php Mysql, php, sql
Mysql Crud, Php Mysql, php, sql
Aimal Miakhel
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
CPD INDIA
 
Operators php
Operators phpOperators php
Operators php
Chandni Pm
 
01 Php Introduction
01 Php Introduction01 Php Introduction
01 Php Introduction
Geshan Manandhar
 
Php with MYSQL Database
Php with MYSQL DatabasePhp with MYSQL Database
Php with MYSQL Database
Computer Hardware & Trouble shooting
 
Inheritance In Java
Inheritance In JavaInheritance In Java
Inheritance In Java
Darpan Chelani
 
PHP variables
PHP  variablesPHP  variables
PHP variables
Siddique Ibrahim
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
Compare Infobase Limited
 
Php
PhpPhp
Php
Shyam Khant
 
Object oriented programming With C#
Object oriented programming With C#Object oriented programming With C#
Object oriented programming With C#
Youssef Mohammed Abohaty
 
Templates in C++
Templates in C++Templates in C++
Templates in C++
Tech_MX
 
Encapsulation C++
Encapsulation C++Encapsulation C++
Encapsulation C++
Hashim Hashim
 
Data Structures
Data StructuresData Structures
Data Structures
Prof. Dr. K. Adisesha
 
Sorting arrays in PHP
Sorting arrays in PHPSorting arrays in PHP
Sorting arrays in PHP
Vineet Kumar Saini
 
Inheritance in c++
Inheritance in c++Inheritance in c++
Inheritance in c++
Paumil Patel
 
C# basics
 C# basics C# basics
C# basics
Dinesh kumar
 

Viewers also liked (20)

Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
Beginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPBeginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHP
Rick Ogden
 
radar
radarradar
radar
Ramasubbu .P
 
Oop in-php
Oop in-phpOop in-php
Oop in-php
Rajesh S
 
Modul 3 Cara Membuat View Pada CodeIgniter
Modul 3 Cara Membuat View Pada CodeIgniterModul 3 Cara Membuat View Pada CodeIgniter
Modul 3 Cara Membuat View Pada CodeIgniter
Riki Afriansyah
 
Modular PHP Development using CodeIgniter Bonfire
Modular PHP Development using CodeIgniter BonfireModular PHP Development using CodeIgniter Bonfire
Modular PHP Development using CodeIgniter Bonfire
Jeff Fox
 
Functions in php
Functions in phpFunctions in php
Functions in php
Mudasir Syed
 
Guide
GuideGuide
Guide
Ramasubbu .P
 
My SQl
My SQlMy SQl
My SQl
Ramasubbu .P
 
sql
sqlsql
sql
Ramasubbu .P
 
IP Addressing
IP AddressingIP Addressing
IP Addressing
Ramasubbu .P
 
Saftey
SafteySaftey
Saftey
Ramasubbu .P
 
Mysql
MysqlMysql
Mysql
Ramasubbu .P
 
1. review jurnal effect dwi hastho
1. review jurnal effect dwi hastho1. review jurnal effect dwi hastho
1. review jurnal effect dwi hastho
Hastho Oke Sekali Jaya
 
I/O Management
I/O ManagementI/O Management
I/O Management
Ramasubbu .P
 
Shell Script
Shell ScriptShell Script
Shell Script
Ramasubbu .P
 
Operating systems
Operating systemsOperating systems
Operating systems
Eduardo Triana
 
Network Security
Network SecurityNetwork Security
Network Security
Ramasubbu .P
 
The Dining Philosophers problem in Bangla
The Dining Philosophers problem in BanglaThe Dining Philosophers problem in Bangla
The Dining Philosophers problem in Bangla
Sifat Hossain
 
MSAT
MSATMSAT
MSAT
Ramasubbu .P
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
Ahmed Swilam
 
Beginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPBeginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHP
Rick Ogden
 
Oop in-php
Oop in-phpOop in-php
Oop in-php
Rajesh S
 
Modul 3 Cara Membuat View Pada CodeIgniter
Modul 3 Cara Membuat View Pada CodeIgniterModul 3 Cara Membuat View Pada CodeIgniter
Modul 3 Cara Membuat View Pada CodeIgniter
Riki Afriansyah
 
Modular PHP Development using CodeIgniter Bonfire
Modular PHP Development using CodeIgniter BonfireModular PHP Development using CodeIgniter Bonfire
Modular PHP Development using CodeIgniter Bonfire
Jeff Fox
 
The Dining Philosophers problem in Bangla
The Dining Philosophers problem in BanglaThe Dining Philosophers problem in Bangla
The Dining Philosophers problem in Bangla
Sifat Hossain
 
Ad

Similar to Class and Objects in PHP (20)

PHP Classes and OOPS Concept
PHP Classes and OOPS ConceptPHP Classes and OOPS Concept
PHP Classes and OOPS Concept
Dot Com Infoway - Custom Software, Mobile, Web Application Development and Digital Marketing Company
 
Oops implemetation material
Oops implemetation materialOops implemetation material
Oops implemetation material
Deepak Solanki
 
SQL Devlopment for 10 ppt
SQL Devlopment for 10 pptSQL Devlopment for 10 ppt
SQL Devlopment for 10 ppt
Tanay Kishore Mishra
 
06-classes.ppt (copy).pptx
06-classes.ppt (copy).pptx06-classes.ppt (copy).pptx
06-classes.ppt (copy).pptx
Thắng It
 
Php Oop
Php OopPhp Oop
Php Oop
mussawir20
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
Chhom Karath
 
OOP
OOPOOP
OOP
thinkphp
 
Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)
Bozhidar Boshnakov
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHP
wahidullah mudaser
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptx
DavidLazar17
 
OOP in PHP.pptx
OOP in PHP.pptxOOP in PHP.pptx
OOP in PHP.pptx
switipatel4
 
14 Defining classes
14 Defining classes14 Defining classes
14 Defining classes
maznabili
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeObject Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
DrDhivyaaCRAssistant
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
DrDhivyaaCRAssistant
 
Intro to The PHP SPL
Intro to The PHP SPLIntro to The PHP SPL
Intro to The PHP SPL
Chris Tankersley
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5
Stephan Schmidt
 
Oops concept in php
Oops concept in phpOops concept in php
Oops concept in php
selvabalaji k
 
Demystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming - Lone Star PHPDemystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming - Lone Star PHP
Alena Holligan
 
Oops implemetation material
Oops implemetation materialOops implemetation material
Oops implemetation material
Deepak Solanki
 
06-classes.ppt (copy).pptx
06-classes.ppt (copy).pptx06-classes.ppt (copy).pptx
06-classes.ppt (copy).pptx
Thắng It
 
Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)
Bozhidar Boshnakov
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHP
wahidullah mudaser
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptx
DavidLazar17
 
14 Defining classes
14 Defining classes14 Defining classes
14 Defining classes
maznabili
 
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering CollegeObject Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Object Oriented PHP by Dr.C.R.Dhivyaa Kongu Engineering College
Dhivyaa C.R
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
Shivam Singhal
 
Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5Go OO! - Real-life Design Patterns in PHP 5
Go OO! - Real-life Design Patterns in PHP 5
Stephan Schmidt
 
Demystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming - Lone Star PHPDemystifying Object-Oriented Programming - Lone Star PHP
Demystifying Object-Oriented Programming - Lone Star PHP
Alena Holligan
 
Ad

More from Ramasubbu .P (20)

Press
PressPress
Press
Ramasubbu .P
 
Milling 2
Milling 2Milling 2
Milling 2
Ramasubbu .P
 
MIlling 1
MIlling 1MIlling 1
MIlling 1
Ramasubbu .P
 
Drillings
DrillingsDrillings
Drillings
Ramasubbu .P
 
Holding
HoldingHolding
Holding
Ramasubbu .P
 
Harvesting
HarvestingHarvesting
Harvesting
Ramasubbu .P
 
Plough
PloughPlough
Plough
Ramasubbu .P
 
Tractor PTO
Tractor PTOTractor PTO
Tractor PTO
Ramasubbu .P
 
Tractor Components
Tractor ComponentsTractor Components
Tractor Components
Ramasubbu .P
 
GPS
GPSGPS
GPS
Ramasubbu .P
 
RTOS
RTOSRTOS
RTOS
Ramasubbu .P
 
Virus
VirusVirus
Virus
Ramasubbu .P
 
Hacker
HackerHacker
Hacker
Ramasubbu .P
 
Denail of Service
Denail of ServiceDenail of Service
Denail of Service
Ramasubbu .P
 
RAID CONCEPT
RAID CONCEPTRAID CONCEPT
RAID CONCEPT
Ramasubbu .P
 
Timer
TimerTimer
Timer
Ramasubbu .P
 
Sequential Logic Circuit
Sequential Logic CircuitSequential Logic Circuit
Sequential Logic Circuit
Ramasubbu .P
 
PL C
PL CPL C
PL C
Ramasubbu .P
 
P L C
P L CP L C
P L C
Ramasubbu .P
 
Ladder
LadderLadder
Ladder
Ramasubbu .P
 

Recently uploaded (20)

The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
Module 1: Foundations of Research
Module 1: Foundations of ResearchModule 1: Foundations of Research
Module 1: Foundations of Research
drroxannekemp
 
Botany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic ExcellenceBotany Assignment Help Guide - Academic Excellence
Botany Assignment Help Guide - Academic Excellence
online college homework help
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
E-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26ASE-Filing_of_Income_Tax.pptx and concept of form 26AS
E-Filing_of_Income_Tax.pptx and concept of form 26AS
Abinash Palangdar
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
UPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guideUPMVLE migration to ARAL. A step- by- step guide
UPMVLE migration to ARAL. A step- by- step guide
abmerca
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
Mental Health Assessment in 5th semester bsc. nursing and also used in 2nd ye...
parmarjuli1412
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
Overview Well-Being and Creative Careers
Overview Well-Being and Creative CareersOverview Well-Being and Creative Careers
Overview Well-Being and Creative Careers
University of Amsterdam
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 

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…
  翻译: