SlideShare une entreprise Scribd logo
1  sur  38
PHP Classes
and
Object Orientation

PHP Workshop

1
Reminder… a function
• Reusable piece of code.
• Has its own ‘local scope’.
function my_func($arg1,$arg2) {
<< function statements >>
}

PHP Workshop

2
Conceptually, what does a
function represent?
…give the function something (arguments), it does
something with them, and then returns a result…

Action or Method

PHP Workshop

3
What is a class?
Conceptually, a class represents an
object, with associated methods
and variables

PHP Workshop

4
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.

PHP Workshop

5
Class Definition
<?php
Define the name
class dog {
of the class.
public $name;
public function bark() {
echo ‘Woof!’;
}
}
?>
PHP Workshop

6
Class Definition
<?php
class dog {
var $name
public $name;
public function bark() {
echo ‘Woof!’;
Define an object
}
attribute (variable),
}
the dog’s name.
?>
PHP Workshop

7
Define
Class Definition an

object action
<?php
(function), the
class dog {
dog’s bark.
public $name;
public function bark() {
function bark() {
echo ‘Woof!’;
echo ‘Woof!’;
}
}
?>
PHP Workshop

8
Class Definition
<?php
class dog {
public $name;
public function bark() {
echo ‘Woof!’;
}
End the class
}
definition
?>
PHP Workshop

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

PHP Workshop

10
Class Usage
<?php
require(‘dog.class.php’);
$puppy = new dog();
$puppy->name = ‘Rover’;
echo “{$puppy->name} says ”;
$puppy->bark();
?>
PHP Workshop

11
Class Usage
<?php
require(‘dog.class.php’);
$puppy = new dog();
$puppy->name = ‘Rover’;
echo “{$puppy->name} says ”;
$puppy->bark();
Include the class
?>
definition
PHP Workshop

12
Class Usage
<?php
require(‘dog.class.php’);
$puppy = new dog();
$puppy->name = ‘Rover’;
echo “{$puppy->name} says ”;
$puppy->bark();
Create a new
instance of the
?>
class.
PHP Workshop

13
Class Usage
<?php
require(‘dog.class.php’);
$puppy = new dog();
$puppy->name = ‘Rover’;
echo “{$puppy->name} says ”;
$puppy->bark();
Set the name
?>
variable of this
instance to
‘Rover’.
PHP Workshop

14
Class Usage
Use the name
<?php
variable of this
require(‘dog.class.php’);
instance in an
$puppy = new dog(); echo statement..
$puppy->name = ‘Rover’;
echo “{$puppy->name} says ”;
$puppy->bark();
?>
PHP Workshop

15
Class Usage
<?php
require(‘dog.class.php’);
$puppy = new dog();
$puppy->name = ‘Rover’;
echo “{$puppy->name} says ”;
$puppy->bark();
Use the dog
?>
object bark
method.
PHP Workshop

16
Class Usage
<?php
require(‘dog.class.php’);
$puppy = new dog();
$puppy->name = ‘Rover’;
echo “{$puppy->name} says ”;
$puppy->bark();
?>
[example file: classes1.php]
PHP Workshop

17
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’;
PHP Workshop

18
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!’;
}
}
PHP Workshop

19
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!
PHP Workshop

20
Constructor Example
<?php
class dog {
Constructor function
public $name;
public function __construct($nametext) {
$this->name = $nametext;
}
public function bark() {
echo ‘Woof!’;
}
}
?>
PHP Workshop

21
Constructor Example
<?php
…
$puppy = new dog(‘Rover’);
…
?>
Constructor arguments
are passed during the
instantiation of the object.

PHP Workshop

22
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..
PHP Workshop

23
Inheritance
• The real power of using classes is the
property of inheritance – creating a
hierarchy of interlinked classes.
dog

parent
children

poodle
PHP Workshop

alsatian
24
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…

PHP Workshop

25
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’;
}
}
}
PHP Workshop

26
Inheritance example
The American Kennel Club (AKC) recognizes three sizes of poodle - Standard,
Miniature, and Toy…

class poodle extends dog {
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’;
}
}

Note the use of the
extends keyword to
indicate that the
poodle class is a child
of the dog class…

}
PHP Workshop

27
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();
…

PHP Workshop

28
…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!’;
}
…
}
PHP Workshop

29
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.
PHP Workshop

30
Objects within Objects
• It is perfectly possible to include objects within another
object..
class dogtag {
public $words;
}
class dog {
public $name;
public $tag;

}

…
$puppy = new dog;
$puppy->name = “Rover";
$poppy->tag = new dogtag;
$poppy->tag->words = “blah”;
…

public function bark() {
echo "Woof!n";
}

PHP Workshop

31
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.

PHP Workshop

32
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

PHP Workshop

33
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();
PHP Workshop

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

PHP Workshop

35
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).

PHP Workshop

36
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!

PHP Workshop

37
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…
PHP Workshop

38

Contenu connexe

Tendances

A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented Php
Michael Girouard
 
Python decorators
Python decoratorsPython decorators
Python decorators
Alex Su
 

Tendances (20)

Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
Introduction in php part 2
Introduction in php part 2Introduction in php part 2
Introduction in php part 2
 
FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3
 
Creating Domain Specific Languages in Python
Creating Domain Specific Languages in PythonCreating Domain Specific Languages in Python
Creating Domain Specific Languages in Python
 
Intermediate OOP in PHP
Intermediate OOP in PHPIntermediate OOP in PHP
Intermediate OOP in PHP
 
A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented Php
 
Introduction in php
Introduction in phpIntroduction in php
Introduction in php
 
PHP Functions & Arrays
PHP Functions & ArraysPHP Functions & Arrays
PHP Functions & Arrays
 
Lists and arrays
Lists and arraysLists and arrays
Lists and arrays
 
Python: Basic Inheritance
Python: Basic InheritancePython: Basic Inheritance
Python: Basic Inheritance
 
PHP Conference Asia 2016
PHP Conference Asia 2016PHP Conference Asia 2016
PHP Conference Asia 2016
 
Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?Typed Properties and more: What's coming in PHP 7.4?
Typed Properties and more: What's coming in PHP 7.4?
 
Python decorators
Python decoratorsPython decorators
Python decorators
 
Fantastic DSL in Python
Fantastic DSL in PythonFantastic DSL in Python
Fantastic DSL in Python
 
Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"Nikita Popov "What’s new in PHP 8.0?"
Nikita Popov "What’s new in PHP 8.0?"
 
Oops in php
Oops in phpOops in php
Oops in php
 
Python decorators
Python decoratorsPython decorators
Python decorators
 
Perl
PerlPerl
Perl
 
PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021PHP Enums - PHPCon Japan 2021
PHP Enums - PHPCon Japan 2021
 

En vedette (10)

PHP- Introduction to Object Oriented PHP
PHP-  Introduction to Object Oriented PHPPHP-  Introduction to Object Oriented PHP
PHP- Introduction to Object Oriented PHP
 
O que esperar do Zend Framework 2
O que esperar do Zend Framework 2O que esperar do Zend Framework 2
O que esperar do Zend Framework 2
 
Desenvolvimento Web com CakePHP
Desenvolvimento Web com CakePHPDesenvolvimento Web com CakePHP
Desenvolvimento Web com CakePHP
 
Workshop: WebSockets com HTML 5 & PHP - Gustavo Ciello
Workshop: WebSockets com HTML 5 & PHP - Gustavo CielloWorkshop: WebSockets com HTML 5 & PHP - Gustavo Ciello
Workshop: WebSockets com HTML 5 & PHP - Gustavo Ciello
 
PHP 5.3 - Estruturas de Controle
PHP 5.3 - Estruturas de ControlePHP 5.3 - Estruturas de Controle
PHP 5.3 - Estruturas de Controle
 
Css Ppt
Css PptCss Ppt
Css Ppt
 
HTML5 for PHP Developers - IPC
HTML5 for PHP Developers - IPCHTML5 for PHP Developers - IPC
HTML5 for PHP Developers - IPC
 
Classroom Objects: PowerPoint Activities
Classroom Objects: PowerPoint ActivitiesClassroom Objects: PowerPoint Activities
Classroom Objects: PowerPoint Activities
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
 

Similaire à SQL Devlopment for 10 ppt

PHP-05-Objects.ppt
PHP-05-Objects.pptPHP-05-Objects.ppt
PHP-05-Objects.ppt
rani marri
 
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
tutorialsruby
 
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
tutorialsruby
 
Oop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.comOop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.com
ayandoesnotemail
 

Similaire à SQL Devlopment for 10 ppt (20)

06-classes.ppt (copy).pptx
06-classes.ppt (copy).pptx06-classes.ppt (copy).pptx
06-classes.ppt (copy).pptx
 
Oops implemetation material
Oops implemetation materialOops implemetation material
Oops implemetation material
 
10 classes
10 classes10 classes
10 classes
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
 
OOP
OOPOOP
OOP
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
 
Phpspec tips&amp;tricks
Phpspec tips&amp;tricksPhpspec tips&amp;tricks
Phpspec tips&amp;tricks
 
Objects, Testing, and Responsibility
Objects, Testing, and ResponsibilityObjects, Testing, and Responsibility
Objects, Testing, and Responsibility
 
Basic Oops concept of PHP
Basic Oops concept of PHPBasic Oops concept of PHP
Basic Oops concept of PHP
 
PHP-05-Objects.ppt
PHP-05-Objects.pptPHP-05-Objects.ppt
PHP-05-Objects.ppt
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 
UNIT III (8).pptx
UNIT III (8).pptxUNIT III (8).pptx
UNIT III (8).pptx
 
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
 
Introduction Php
Introduction PhpIntroduction Php
Introduction Php
 
PHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptxPHP OOP Lecture - 02.pptx
PHP OOP Lecture - 02.pptx
 
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
 
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
 
Oop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.comOop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.com
 
Oop in php tutorial
Oop in php tutorialOop in php tutorial
Oop in php tutorial
 
OpenGurukul : Language : PHP
OpenGurukul : Language : PHPOpenGurukul : Language : PHP
OpenGurukul : Language : PHP
 

Dernier

Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
AnaAcapella
 

Dernier (20)

How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
Spellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please PractiseSpellings Wk 3 English CAPS CARES Please Practise
Spellings Wk 3 English CAPS CARES Please Practise
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptxSKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
SKILL OF INTRODUCING THE LESSON MICRO SKILLS.pptx
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 

SQL Devlopment for 10 ppt

  • 2. Reminder… a function • Reusable piece of code. • Has its own ‘local scope’. function my_func($arg1,$arg2) { << function statements >> } PHP Workshop 2
  • 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 PHP Workshop 3
  • 4. What is a class? Conceptually, a class represents an object, with associated methods and variables PHP Workshop 4
  • 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. PHP Workshop 5
  • 6. Class Definition <?php Define the name class dog { of the class. public $name; public function bark() { echo ‘Woof!’; } } ?> PHP Workshop 6
  • 7. Class Definition <?php class dog { var $name public $name; public function bark() { echo ‘Woof!’; Define an object } attribute (variable), } the dog’s name. ?> PHP Workshop 7
  • 8. Define Class Definition an object action <?php (function), the class dog { dog’s bark. public $name; public function bark() { function bark() { echo ‘Woof!’; echo ‘Woof!’; } } ?> PHP Workshop 8
  • 9. Class Definition <?php class dog { public $name; public function bark() { echo ‘Woof!’; } End the class } definition ?> PHP Workshop 9
  • 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… PHP Workshop 10
  • 11. Class Usage <?php require(‘dog.class.php’); $puppy = new dog(); $puppy->name = ‘Rover’; echo “{$puppy->name} says ”; $puppy->bark(); ?> PHP Workshop 11
  • 12. Class Usage <?php require(‘dog.class.php’); $puppy = new dog(); $puppy->name = ‘Rover’; echo “{$puppy->name} says ”; $puppy->bark(); Include the class ?> definition PHP Workshop 12
  • 13. Class Usage <?php require(‘dog.class.php’); $puppy = new dog(); $puppy->name = ‘Rover’; echo “{$puppy->name} says ”; $puppy->bark(); Create a new instance of the ?> class. PHP Workshop 13
  • 14. Class Usage <?php require(‘dog.class.php’); $puppy = new dog(); $puppy->name = ‘Rover’; echo “{$puppy->name} says ”; $puppy->bark(); Set the name ?> variable of this instance to ‘Rover’. PHP Workshop 14
  • 15. Class Usage Use the name <?php variable of this require(‘dog.class.php’); instance in an $puppy = new dog(); echo statement.. $puppy->name = ‘Rover’; echo “{$puppy->name} says ”; $puppy->bark(); ?> PHP Workshop 15
  • 16. Class Usage <?php require(‘dog.class.php’); $puppy = new dog(); $puppy->name = ‘Rover’; echo “{$puppy->name} says ”; $puppy->bark(); Use the dog ?> object bark method. PHP Workshop 16
  • 17. Class Usage <?php require(‘dog.class.php’); $puppy = new dog(); $puppy->name = ‘Rover’; echo “{$puppy->name} says ”; $puppy->bark(); ?> [example file: classes1.php] PHP Workshop 17
  • 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’; PHP Workshop 18
  • 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!’; } } PHP Workshop 19
  • 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! PHP Workshop 20
  • 21. Constructor Example <?php class dog { Constructor function public $name; public function __construct($nametext) { $this->name = $nametext; } public function bark() { echo ‘Woof!’; } } ?> PHP Workshop 21
  • 22. Constructor Example <?php … $puppy = new dog(‘Rover’); … ?> Constructor arguments are passed during the instantiation of the object. PHP Workshop 22
  • 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.. PHP Workshop 23
  • 24. Inheritance • The real power of using classes is the property of inheritance – creating a hierarchy of interlinked classes. dog parent children poodle PHP Workshop alsatian 24
  • 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… PHP Workshop 25
  • 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’; } } } PHP Workshop 26
  • 27. Inheritance example The American Kennel Club (AKC) recognizes three sizes of poodle - Standard, Miniature, and Toy… class poodle extends dog { 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’; } } Note the use of the extends keyword to indicate that the poodle class is a child of the dog class… } PHP Workshop 27
  • 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(); … PHP Workshop 28
  • 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!’; } … } PHP Workshop 29
  • 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. PHP Workshop 30
  • 31. Objects within Objects • It is perfectly possible to include objects within another object.. class dogtag { public $words; } class dog { public $name; public $tag; } … $puppy = new dog; $puppy->name = “Rover"; $poppy->tag = new dogtag; $poppy->tag->words = “blah”; … public function bark() { echo "Woof!n"; } PHP Workshop 31
  • 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. PHP Workshop 32
  • 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 PHP Workshop 33
  • 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(); PHP Workshop 34
  • 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… PHP Workshop 35
  • 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). PHP Workshop 36
  • 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! PHP Workshop 37
  • 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… PHP Workshop 38

Notes de l'éditeur

  1. Remind them about functions to start off with.. hopefully they all agree that they are useful.
  2. Say we’ll talk more about the application of OBJECTS and object orientated programming later, for now we are going to learn in practice how to use the things..
  3. Demonstrate with file classes1.php
  4. Show example in code.
  5. demonstrate…