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.
5.
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âŚ
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.
21.
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.
25.
26.
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(); âŚ