1 / 18

PHP Classes and Object Oriented Programming

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

bevan
Télécharger la présentation

PHP Classes and Object Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. PHP Classes and Object Oriented Programming

  2. What is a class? A class is a template for creating objects, a “cookie cutter”. A class defines functions (methods)and data (attributes)

  3. What is an object? • An instance of a class • A “cookie”

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

  5. Class Syntax • Class name should be capitalized • File name should be same as class name • Use “required” to include the class file

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

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

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

  9. 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”);

  10. Constructor Example <?php class Dog { var $_name; public function Dog($nametext) { $this->name = $nametext; } } ?> See Dog3.php

  11. 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!

  12. Inheritance • The real power of using classes is the property of inheritance – creating a hierarchy of related classes. Dog parent children Poodle Dalmatian

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

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

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

  16. 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();

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

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

More Related