This document provides an overview of object-oriented programming fundamentals in PHP, including classes, objects, inheritance, methods, properties, constructors, destructors, visibility, constants, interfaces, abstract classes, and exceptions. Key concepts covered are class declaration, object instantiation, class inheritance that allows extending and overriding methods and properties, defining and calling methods and properties, constructors and destructors, visibility levels for properties and methods, declaring and accessing properties, static methods and properties, class constants, interfaces for specifying APIs, abstract classes for defining basic skeletons, and exceptions for error control.
3. CLASS DECLARATION
The basic declaration of a class is very simple:
class myClass {
// Class contents go here
}
4. OBJECT INSTANTIATION
This is done by using the new construct:
$myClassInstance = new myClass();
An object is always passed by reference rather than by value.
$myClassInstance = new myClass();
$copyInstance = $myClassInstance();
5. CLASS INHERITANCE
Allows a class to extend another class, essentially adding new methods and properties, as well as
overriding existing ones as needed.
class a {
function test()
{
echo "a::test called";
}
function func()
{
echo "a::func called";
}
}
class b extends a {
function test()
{
echo "b::test called";
}
}
class c extends b {
function test()
{
parent::test();
}
}
class d extends c {
function test()
{
b::test();
}
}
$a = new a();
$b = new b();
$c = new c();
$d = new d();
6. METHODS AND PROPERTIES
Methods are declared just like traditional functions:
class myClass {
function myFunction() {
echo "You called myClass::myFunction";
}
}
From outside the scope of a class, its methods are called using the indirection operator ->:
$obj = new myClass();
$obj->myFunction();
7. METHODS AND PROPERTIES
class myClass {
function myFunction($data) {
echo "The value is $data";
}
function callMyFunction($data) {
// Call myFunction()
$this->myFunction($data);
}
}
$obj = new myClass();
$obj->callMyFunction(123);
9. DESTRUCTORS
class foo {
function __construct()
{
echo __METHOD__ . PHP_EOL;
}
function __destruct()
{
echo __METHOD__;
}
}
new foo();
This code will display:
foo::__construct
foo::__destruct
10. VISIBILITY
public
The resource can be accessed from any scope.
protected
The resource can only be accessed from within the class where it is
defined and its descendants.
private
The resource can only be accessed from within the class where it is
defined.
The resource is accessible from any scope, but cannot be
overridden in descendant classes.
final
11. DECLARING AND ACCESSING PROPERTIES
class foo {
public $bar;
protected $baz;
private $bas;
public $var1 = "Test"; // String
public $var2 = 1.23; // Numeric value
public $var3 = array (1, 2, 3);
}
12. CONSTANTS, STATIC METHODS AND
PROPERTIES
class foo {
static $bar = "bat";
public static function baz()
{
echo "Hello World";
}
}
$foo = new foo();
$foo->baz();
echo $foo->bar;
Hello WorldPHP Strict Standards: Accessing static
property foo::$bar as non static in PHPDocument1
on line 17
Strict Standards: Accessing static property
foo::$bar as non static in PHPDocument1 on line 1
14. INTERFACES AND ABSTRACT CLASSES
•An abstract class essentially defines the basic skeleton of a
specific type of encapsulated entity.
•Interfaces, on the other hand, are used to specify an API that
a class must implement.
15. EXCEPTIONS
• Exceptions provide an error control mechanism that is more fine-grained than
traditional PHP fault handling, and that allows for a much greater degree of control.
• Key differences between “regular” PHP errors and exceptions:
• Exceptions are objects, created (or “thrown”) when an error occurs
• Exceptions can be handled at different points in a script’s execution, and different
types of exceptions can be handled by separate portions of a script’s code
• All unhandled exceptions are fatal
• Exceptions can be thrown from the __construct method on failure
• Exceptions change the flow of the application
Notes de l'éditeur
OOP revolves around the concept of grouping code and data together in logical units called classesThis process is usually referred to as encapsulation, or information hidingClasses are essentially a representation of a set of functions (also called methods) and variables (called properties) designed to work together and to provide a specific interface to the outside worldClasses are just blueprints that must be instantiated into objects
This advises the PHP interpreter that you are declaring a class called myClass whose contents will normally be a combination of constants, variables and functions (called methods)
How do you reference a class’ method from within the class itself?PHP defines a special variable called $this; this variable is only defined within an object’s scope, and always points to the object itself
PHP 5 now uses the magic __construct() method as the constructor for any class regardless of the class’ nameThis example will display foo::__construct (the__METHOD__constant is replaced at compilation time with the name of the current class method). Note that, if the __construct() method is not found, PHP will look for the old PHP 4-style constructor (foo) and call that instead.
This works like a mirror image of __construct(): it is called right before an object is destroyed, and is useful for performing cleanup procedures—such as disconnecting from a remote resource, or deleting temporary files
N.B. The final visibility level only applies to methods and classes. Classes that are declared as final cannot be extended.
Note that, like a normal variable, a class property can be initialized while it is being declared. However, the initialization is limited to assigning values (but not by evaluating expressions). You can’t, for example, initialize a variable by calling a function—that’s something you can only do within one of the class’ methods (typically, the constructor).
PHP is very strict about the use of static properties; calling static properties using object notation (i.e. $obj->property) will result in both a “strict standards” message and a notice.
Class constants work in the same way as regular constants, except they are scoped within a class. Class constants are public, and accessible from all scopes; for example, the following script will output Hello World
An abstract class essentially defines the basic skeleton of a specific type of encapsulated entity—for example, you can use an abstract class to define the basic concept of “car” as having two doors, a lock and a method that locks or unlocks the doors. Abstract classes cannot be used directly, but they must be extended so that the descendent class provides a full complement of methods.Interfaces, on the other hand, are used to specify an API that a class must implement. This allows you to create a common “contract” that your classes must implement in order to satisfy certain logical requirements—for example, you could use interfaces to abstract the concept of database provider into a common API that could then be implemented by a series of classes that interface to different DBMSs