1 of 27

Chapter 9: Object oriented programming with PHP

  • General concept
  • Object vs Class
  • Class construction

2 of 27

Why objects?

  • PHP scripts become more and more complex, the difficulty of maintaining them increases dramatically especially if you are programming in a procedural style which is < php4.
  • php4 and php5 have been completely redesigned to meet OOP evolution.

3 of 27

Creating basic classes

  • A class can be thought of as a blueprint of an object and defines all the actions that object is capable of.
  • Class definitions thus contain variables, functions (called methods), and even constants that are specific to that class or its instances only

4 of 27

Implementation of OOP

  • Create a class

  • Create an instance of class

5 of 27

Implementation of OOP

  • Accessing the instance object

  • calls the my_method() method of the class, the following output will be generated as:

Hello, you called my_method(MyParam)!

The value of my variable is 10

6 of 27

Private and Public

  • Public : Class members can be accessed from anywhere within a script.
    • can be called or modified internally by the object or from outside the object.

7 of 27

Private and Public

  • Private: access to the class member only from within an instance of that class through the $this variable

8 of 27

Constructors and Destructors

  • Functions that are called when a new instance of an object is created (constructors) and/or destroyed (destructors).
  • Purpose is to allow for a means to initialize and clean up after an object during its use.

9 of 27

Class Constants

  • A facility to define constant values within a class definition

10 of 27

Static Methods

  • Methods that are part of a class, but that are designed to be called from outside the context of the instance of an object.
  • Cannot use the $this variable to reference the current instance of the object
  • To create a static method, add the static keyword in front of any class method declaration:

static function myMethod() {

... }

  • To call a static method, the syntax is as follows:

<CLASSNAME>::<METHOD>

11 of 27

Class Inheritance

  • Capability for one class definition to extend another class definition's functionality.
  • When one class inherits from another, all of the parent's methods, properties, and constants are available from the child class as well.
  • Child classes can also reimplement some or all of the methods, properties, and constants of a parent class to provide additional or different functionality.
  • Classes are inherited from one another using the extends keyword in the class definition.

12 of 27

Example inheritance

13 of 27

Class member overloading

  • When the preceding script is executed, what will happen?

14 of 27

Class Member Binding

  • When the callMe() method is called, what will happen?

15 of 27

Advanced classes

  • Abstract Classes and Methods
    • To provide a superclass that defines the abstract characteristics of classes that inherit from it.
    • May or may not actually contain any code within them, and they can never be instantiated directly.

16 of 27

Example Abstract Classes

17 of 27

Interfaces

  • Designed to assure functionality within a class.
  • To define a set of methods a class claiming to implement that interface must have.
  • Example

18 of 27

Implementing Interfaces

  • A class that implements an interface

19 of 27

Final Classes and Methods

  • Used to provide control over inheritance.
  • Classes or methods declared as final cannot be extended and/or overloaded by child classes.
  • To ensure that a particular class or method is never overloaded, �simply add the �final keyword to �the method or �class definition

20 of 27

Special methods

  • Getters and Setters
    • to provide a catch-all interface when accessing properties in objects.
  • The __call() Method
    • used to provide a catch-all for method calls within an object.
  • The __toString() Method
    • to provide an easy way to access the string representation of a complex object.

21 of 27

Getters and Setters

  • These methods, named __get() and __set() respectively, will be called when a particular property was not defined.
  • The prototype for these special methods are as follows:

  • Getter and setter methods are useful when dealing with Web services or container objects where the properties available within an instance of a class may not be completely known until the execution of the script.

function __get($name);

function __set($name, $value);

22 of 27

The __call() Method

  • When an attempt to call a method not previously defined within the class is received, if possible the __call method will be called.
  • The prototype of this method is as follows:

  • useful when a complete function list is not available until the execution of the script.
  • another application of the __call() method is to provide a catch-all method to handle attempts to call invalid methods

function __call($method, $arguments);

23 of 27

Using the __call() Method

24 of 27

The __toString() Method

  • When defined, PHP will call this method under certain circumstances when the object would be better treated as a string
  • The value of the string returned by the __toString() method can be anything

25 of 27

Using the __toString() Method

26 of 27

Class autoloading

  • To specify a function to load classes as they are needed, if desired.
  • This function is called the __autoload() function and has a prototype as follows:

function __autoload($classname);

27 of 27

Using the __autoload() Function

  • This example used to load classes by looking up their location in a predefined associative array. Because neither MyClass nor anotherClass has been defined yet in this script, the __autoload() function will be called in both instances to give the script an opportunity to find the class itself