OOP and PHP

From Wiki

Jump to: navigation, search

How object-oriented is PHP?

  • Single inheritance. PHP allows a class definition to inherit from another class, using the extends clause. Both properties and functions are inherited.
  • Multiple inheritance. PHP do not offers support for multiple inheritance and no notion of interface inheritance as in Java. Each class inherits from, at most, one parent class (though a class may implement many interfaces).
  • Constructors. Any class can have one constructor function, which in PHP is named __construct(). Note that there are two underscore characters at the front of that function name. Constructors of parent classes are not automatically called but must be invoked explicitly.
  • Destructors. PHP supports explicit destructor functions as of version 5. The destructor function of a class is always named __destruct().
  • Encapsulation/access control. PHP supports public, private, and protected properties and methods as of version 5.
  • Polymorphism/overloading. PHP supports polymorphism in the sense that allows instances from subclasses to be used in place of parent instances. The correct method will be dispatched to at runtime. There is no support for method overloading, where dispatch happens based on the method's signature-each class only has one method of a given name.
  • Static (or class) functions. PHP offers static properties and static functions as of version 5. It is also possible to call methods via the Classname::function() syntax.
  • Introspection. PHP offers a wide variety of introspection functions, including the capability to recover class names, function names, and properties names from an instance.

Contents

PHP Classes

Public, Private, and Protected Members

Unless you specify otherwise, properties and functions of a class are public. That is to say, they may be accessed in three possible situations:

  1. From outside the class in which it is declared;
  2. From within the class in which it is declared;
  3. From within another class that implements the class in which it is declared;

If you wish to limit the accessibility of the members of a class, you should use private or protected. By designating a member private, you limit its accessibility to the class in which it is declared. The private member cannot be referred to from classes that inherit the class in which it is declared and cannot be accessed from outside the class.

class MyClass {
 private $colorOfSky = "blue";
 $nameOfShip = "Java Star";
 function __construct($incomingValue) {
  // Statements here run every time an instance of the class is created.
 }
 function myPublicFunction ($myInput) {
  return("I'm visible!");
 }
 private function myPrivateFunction ($myInput) {
  global $myGlobal;
  return($myGlobal * $myInput * myFunction($this->myProperty));
 }
}

A protected property or method is accessible in the class in which it is declared, as well as in classes that extend that class. Protected members are not available outside of those two kinds of classes, however.

class MyClass {
 protected $colorOfSky = "blue";
 $nameOfShip = "Java Star";
 function __construct($incomingValue) {
  // Statements here run every time an instance of the class is created.
 }
 function myPublicFunction ($myInput) {
  return("I'm visible!");
 }
 protected function myProtectedFunction ($myInput) {
  global $myGlobal;
  return($myGlobal * $myInput * myFunction($this->myProperty));
 }
}

Constants

A constant is somewhat like a variable, in that it holds a value, but is really more like a function because a constant is immutable. Once you declare a constant, it does not change.

class MyClass {
 const REQUIRED_MARGIN = 1.3;
 function __construct($incomingValue) {
  // Statements here run every time an instance of the class is created.
 }
}

In that class, REQUIRED_MARGIN is a constant. It is declared with the keyword const, and under no circumstances can it be changed to anything other than 1.3. Note that the constants name does not have a leading $, as variable names do.

Abstract Classes

An abstract class is one that be inherited but cannot be instantiated, . You declare an abstract class with the keyword abstract:

abstract class MyAbstractClass {
 abstract function myAbstractFunction() {
 }
}

Note that function definitions inside an abstract class must also be preceded by the keyword abstract. It is not legal to have abstract function definitions inside a non-abstract class.

Interfaces

In large object-oriented projects, there is some advantage to be realized in having standard names for methods that do certain work. In PHP5, it is also possible to define an interface:

interface Mail {
 public function sendMail();
}

then, if another class implemented that interface:

class Report implements Mail {
 // Definition goes here
}

it would be required to have a method named sendMail. It's an aid to standardization.

Simulating class functions

Calling parent functions

Calling parent constructors

The special name parent

Serialization

Introspection

Bibliography

  • PHP web site, http://www.php.net
  • PEAR web site, http://www.pear.php.net
  • Tim Converse,Joyce Park, Clark Morgan, PHP5 and MySQL Bible, Wiley Publishing, Inc., 2004.
  • David Sklar, Learning PHP 5, O'Reilly 2004.
  • David Lane, Hugh E. Williams , Web Database Application with PHP and MySQL, 2nd Edition, O'Reilly 2004.
Personal tools