420 likes | 967 Views
PHP Classes and Object Oriented Programming. What is a class ?. A class is a template for creating objects, a “cookie cutter” . A class defines functions (methods) and data (attributes). What is an object?. An instance of a class A “cookie”. What is an object?.
E N D
What is a class? A class is a template for creating objects, a “cookie cutter”. A class defines functions (methods)and data (attributes)
What is an object? • An instance of a class • A “cookie”
What is an object? • Each object has its own data • The object’s functionality is defined by the class methods • Data: • Name • Breed • Weight • Methods: • Bark • Fetch • Sleep
Class Syntax • Class name should be capitalized • File name should be same as class name • Use “required” to include the class file
Dog.php class Dog { var $_name; var $_breed; var $_weight; public function __set($name, $value) { $this->$name = $value; } public function __get($name) { return $this->$name; } public function bark() { echo 'Woof!'; } } class name data functions (methods)
Using a Class myPuppy.php <?php require 'Dog.php'; $puppy = new Dog(); $puppy->__set('name', 'Sammy'); $puppy->__set('weight', 7.5); $puppy->__set('breed', 'German Shepherd'); echo $puppy->__get('name') . ' is a ' ; echo $puppy->__get('weight') . ' pound ‘; echo $puppy->__get('breed') . '<br />'; $puppy->bark(); ?> Construct (instantiate)a dog object
Using class data within the class.. • If you need to access class data within a class function, use the special variable $this • $this refers to the “current object” class Dog { var $_name; public function bark() { echo $this->name.' says Woof!'; } } Note: there is no $ before the attribute name
Constructor methods • A constructor method is automatically executed when the class is instantiated • The constructor always has the same name as the class • If the constructor requires arguments, they must be passed when the class is instantiated $puppy = new Dog(); $puppy = new Dog(“Fido”);
Constructor Example <?php class Dog { var $_name; public function Dog($nametext) { $this->name = $nametext; } } ?> See Dog3.php
Constructing Objects Constructor arguments are passed when the object is instantiated. <?php require 'Dog.php'; $puppy1 = new Dog('Johnny'); $puppy2 = new Dog('Frankie'); $puppy1->bark(); $puppy2->bark(); ?> Remember… each object has its own data Johnny says Woof!Frankie says Woof!
Inheritance • The real power of using classes is the property of inheritance – creating a hierarchy of related classes. Dog parent children Poodle Dalmatian
Inheritance • The child classes ‘inherit’ all the methods and variables of the parent class • The child class can add additional data and methods • The child class can replace parent methods (method overriding)
The American Kennel Club (AKC) recognizes three sizes of poodle - Standard, Miniature, and Toy… Poodle.php class Poodle extends Dog { var $_type; public function set_type($height) { if ($height < 10) { $this->type = ‘Toy’; } elseif ($height > 15) { $this->type = ‘Standard’; } else { $this->type = ‘Miniature’; } } public functionbark() { echo $this->name . ' says Yip!<br />'; } } new data new method overridden method
Inheritance example … $puppy4 = new Poodle(‘Frannie’); $puppy4->set_type(12); // 12 inches high! echo‘Poodle is called ’ . $puppy4->name; echo‘ of type’ . $puppy4->type . ‘<br />’; $puppy4->bark(); …
Why Use Objects? 1. Once you have your head round the concept of objects, intuitively named object orientated code becomes easy to understand. $order->display_basket(); $user->pay($order); $order->display_status();
Why Object Orientate? 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? 3. New code becomes much quicker to write once you have a suitable class library. New classes can be created by extending existing classes. A lot of high quality code is distributed as classes (e.g. http://pear.php.net).