In this part of the PHP tutorial, we will talk about object oriented programming in PHP.
There are three widely used programming paradigms there. Procedural programming, functional programming and object-oriented programming. PHP 5 supports both procedural and object-oriented programming. Earlier versions of PHP had limited or no support for OOP.
Object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design applications and computer programs. (Wikipedia)
There are some basic programming concepts in OOP:
There are two steps in creating an object. First, we create a class. A class is a template for an object. It is a blueprint, which describes the state and behavior that the objects of the class all share. A class can be used to create many objects. Objects created at runtime from a class are called instances of that particular class.
Access modifiers protect data against accidental modifications. They make the programs more robust.
What is method overloading good for? The Qt4 library gives a nice example for the usage. The QPainter class has three methods to draw a rectangle. Their name is drawRect() and their parameters differ. One takes a reference to a floating point rectangle object, another takes a reference to an integer rectangle object and the last one takes four parameters, x, y, width, height. If the C++ language, which is the language in which Qt is developed, didn't have method overloading, the creators of the library would have to name the methods like drawRectRectF(), drawRectRect(), drawRectXYWH(). The solution with method overloading is more elegant.
To simulate method overloading in PHP, we use the
Constructors often take arguments.
In the next example, we initiate data members of the class. Initiation of variables is a typical job for constructors.
A more complex example follows.
Unlike interfaces, abstract classes may have methods with full implementation and may also have defined member fields. So abstract classes may provide a partial implementation. Programmers often put some common functionality into abstract classes. And these abstract classes are later subclassed to provide more specific implementation. For example, the Qt graphics library has a QAbstractButton, which is the abstract base class of button widgets, providing functionality common to buttons. Buttons Q3Button, QCheckBox, QPushButton, QRadioButton, and QToolButton inherit from this base abstract class.
Formally put, abstract classes are used to enforce a protocol. A protocol is a set of operations, which all implementing objects must support.
Interfaces are:
From the second point of view, interfaces are contracts. If agreed upon, they must be followed. They are used to design an architecture of an application. They help organize the code.
Interfaces are fully abstract types. They are declared using the
Interfaces are used to simulate multiple inheritance. A PHP class can extend only one class. A PHP class can implement multiple interfaces. Multiple inheritance using the interfaces is not about inheriting methods and variables. It is about inheriting ideas or contracts, which are described by the interfaces.
There is one important distinction between interfaces and abstract classes. Abstract classes provide partial implementation for classes, that are related in the inheritance hierarchy. Interfaces on the other hand can be implemented by classes, that are not related to each other. For example, we have two buttons. A classic button and a round button. Both inherit from an abstract button class, that provides some common functionality to all buttons. Implementing classes are related, since all are buttons. Another example might have classes Database and SignIn. They are not related to each other. We can apply an ILoggable interface, that would force them to create a method to do logging.
The next example shows, how a class can implement multiple interfaces.
The next example shows how interfaces can extend from multiple other interfaces.
In general, polymorphism is the ability to appear in different forms. Technically, it is the ability to redefine methods for derived classes. Polymorphism is concerned with the application of specific implementations to an interface or a more generic base class.
This was the first part of the description of OOP in PHP 5.
There are three widely used programming paradigms there. Procedural programming, functional programming and object-oriented programming. PHP 5 supports both procedural and object-oriented programming. Earlier versions of PHP had limited or no support for OOP.
Object-oriented programming (OOP) is a programming paradigm that uses objects and their interactions to design applications and computer programs. (Wikipedia)
There are some basic programming concepts in OOP:
- Abstraction
- Polymorphism
- Encapsulation
- Inheritance
Objects
Objects are basic building blocks of a PHP OOP program. An object is a combination of data and methods. In a OOP program, we create objects. These objects communicate together through methods. Each object can receive messages, send messages and process data.There are two steps in creating an object. First, we create a class. A class is a template for an object. It is a blueprint, which describes the state and behavior that the objects of the class all share. A class can be used to create many objects. Objects created at runtime from a class are called instances of that particular class.
<?phpIn our first example, we create a simple object.
class Simple {}
$object = new Simple();
print_r($object);
echo gettype($object), "\n";
?>
class Simple {}This is a simple class definition. The body of the template is empty. It does not have any data or methods.
$object = new Simple();We create a new instance of the Simple class. For this we have the
new
keyword. The $object variable is the handle to the created object. print_r($object);We use the
echo gettype($object), "\n";
print_r()
function to get information about the object and the gettype()
function, to get the type of the variable. $ php class.phpWe don't get much info, since the class definition was empty. The type of the variable is
Simple Object
(
)
object
object
. Object attributes
Object attributes is the data bundled in an instance of a class. The object attributes are called instance variables or member fields. An instance variable is a variable defined in a class, for which each object in the class has a separate copy.<?phpIn the above PHP script, we have a Person class with one member field.
class Person {
public $name = "";
}
$p1 = new Person();
$p1->name = "Jane";
$p2 = new Person();
$p2->name = "Beky";
echo $p1->name . "\n";
echo $p2->name . "\n";
?>
$p1 = new Person();We create an instance of the Person class. And set the $name variable to "Jane". We use the -> operator to access the attributes of objects.
$p1->name = "Jane";
$p2 = new Person();We create another instance of the Person class. Here we set the variable to "Beky".
$p2->name = "Beky";
echo $p1->name . "\n";We print the contents of the variables to the console.
echo $p2->name . "\n";
$ php memberfields.phpWe see the output of the script. Each instance of the Person class has a separate copy of the $name member field.
Jane
Beky
Methods
Methods are functions defined inside the body of a class. They are used to perform operations with the attributes of our objects. Methods are essential in encapsulation concept of the OOP paradigm. For example, we might have a connect() method in our AccessDatabase class. We need not to be informed, how exactly the method connect() connects to the database. We only know, that it is used to connect to a database. This is essential in dividing responsibilities in programming. Especially in large applications.<?phpIn the code example, we have a Circle class. We define two methods.
class Circle {
public $radius;
function setRadius($radius) {
$this->radius = $radius;
}
function area() {
return $this->radius * $this->radius * M_PI;
}
}
$c = new Circle();
$c->setRadius(5);
echo $c->area(), "\n";
?>
public $radius;We have one member field. It is the radius of the circle. The
public
keyword is an access specifier. It tells that the variable is fully accessible from the outside world. function setRadius($radius) {This is the setRadius() method. It is a normal PHP function. We call functions defined inside classes methods. The
$this->radius = $radius;
}
$this
variable is a special variable, which we use to access the member fields from methods. function area() {The area() method returns the area of a circle. The
return $this->radius * $this->radius * M_PI;
}
M_PI
is a built-in constant. $ php circle.phpRunning the example.
78.5398163397
Access modifiers
Access modifiers set the visibility of methods and member fields. PHP 5 has three access modifiers.public
, protected
and private
. public
members can be accessed from anywhere. protected
members can be accessed only within the class itself and by inherited and parent classes. private
members may only be accessed by the class that defines the member. Access modifiers protect data against accidental modifications. They make the programs more robust.
<?phpIn the above PHP script, we have two member fields. One is declared public, the other private.
class Person {
public $name = "";
private $age;
}
$p = new Person();
$p->name = "Jane";
#$p->age = 17;
echo $p->name . "\n";
?>
$p->name = "Jane";We access the $name member from the outside world. By the outside world, we mean 'not in the class'. It is OK, since the $name variable is declared
#$p->age = 17;
public
. Accessing the $age member is not possible. The private
modifier prohibits this. If we uncomment the code line, we will get the 'Fatal error: Cannot access private property Person::$age' error. <?phpIn this PHP script, we have a Derived class, which extends the Base class. The Base class has three member fields. All with different access modifiers. The $is_defined member is not inherited. The
class Base {
public $name = "Base";
protected $id = 6124;
private $is_defined = "yes";
}
class Derived extends Base {
public function info() {
echo "This is Derived class\n";
echo "Members inherited: \n";
echo $this->name . "\n";
echo $this->id . "\n";
echo $this->is_defined . "\n";
}
}
$derived = new Derived();
$derived->info();
?>
private
modifier prevents this. public function info() {The info() method has a
public
access modifier. This means, it can be called outside the class environment. $ php access.phpRunning the PHP script, we receive this output. The public and protected members are inherited, the private member is not.
This is Derived class
Members inherited:
Base
6124
$
<?phpIn this PHP script, we have a SysInfo class. It outputs some system information to the console. We have two private functions and one public. The private methods are here only for internal working of the SysInfo class. They are not meant to be called outside the class.
class SysInfo {
private function get_date() {
return date("Y/m/d");
}
private function get_version() {
return phpversion();
}
public function getInfo() {
$date = $this->get_date();
$version = $this->get_version();
echo "The date is: $date\n";
echo "The PHP version is: $version\n";
}
}
$sys = new SysInfo();
$sys->getInfo();
#$sys->get_date();
?>
$sys = new SysInfo();We create an instance of the SysInfo class and call the publicly available getInfo() method. The getInfo() method uses internally the private methods to do its job. Uncommenting the last code line yields to error.
$sys->getInfo();
#$sys->get_date();
Method overloading
Method overloading allows the creation of several methods with the same name which differ from each other in the type of the input.What is method overloading good for? The Qt4 library gives a nice example for the usage. The QPainter class has three methods to draw a rectangle. Their name is drawRect() and their parameters differ. One takes a reference to a floating point rectangle object, another takes a reference to an integer rectangle object and the last one takes four parameters, x, y, width, height. If the C++ language, which is the language in which Qt is developed, didn't have method overloading, the creators of the library would have to name the methods like drawRectRectF(), drawRectRect(), drawRectXYWH(). The solution with method overloading is more elegant.
<?phpThis is a method overloading, we know from languages like C#, Java or C++. But this does not work in PHP. Running this example, we get the following error: 'Fatal error: Cannot redeclare Sum::getSum()'. PHP functions can take arbitrary number of variables by default.
class Sum {
public function getSum() {
return 0;
}
public function getSum($x) {
return $x;
}
public function getSum($x, $y) {
return $x + $y;
}
}
$s = new Sum();
echo $s->getSum() . "\n" ;
echo $s->getSum(5) . "\n" ;
echo $s->getSum(3, 4) . "\n" ;
?>
To simulate method overloading in PHP, we use the
func_get_args()
function. <?phpThis time, the script will run.
class Sum {
public function getSum() {
$args = func_get_args();
if (empty($args)) return 0;
foreach ($args as $arg) {
$sum += $arg;
}
return $sum;
}
}
$s = new Sum();
echo $s->getSum() . "\n" ;
echo $s->getSum(5) . "\n" ;
echo $s->getSum(3, 4) . "\n" ;
echo $s->getSum(3, 4, 7) . "\n" ;
?>
$args = func_get_args();The
func_get_args()
function returns an array comprising a function's argument list. foreach ($args as $arg) {We go throught all members of the array, and calculate the sum.
$sum += $arg;
}
echo $s->getSum() . "\n" ;We call the same method name with different number of inputs.
echo $s->getSum(5) . "\n" ;
echo $s->getSum(3, 4) . "\n" ;
echo $s->getSum(3, 4, 7) . "\n" ;
The constructor
A constructor is a special kind of a method. It is automatically called, when the object is created. The purpose of the constructor is to initiate the state of the object. The name of the constructor in PHP 5 is always__construct()
. (Two underscores) <?phpWe have a Song class. This class has a constructor, that prints message to the console.
class Song {
function __construct() {
echo "Song object is created \n";
}
}
$song = new Song();
?>
$song = new Song();This is the time, when the object is created and the constructor is called. We get a message in the console.
$ php constructor.phpThis is the output of the script.
Song object is created
Constructors often take arguments.
<?phpWe modify the previous example a bit. We pass a value to the constructor.
class Song {
function __construct($song) {
echo "Song $song is created \n";
}
}
$song = new Song("Bad romance");
?>
function __construct($song) {The passed argument is stored in the local $song variable.
echo "Song $song is created \n";
}
$ php constructor2.phpNow we have a message with a song title printed to the console.
Song Bad romance is created
In the next example, we initiate data members of the class. Initiation of variables is a typical job for constructors.
<?phpWe have a Friend class with data members and methods.
class Friend {
private $born;
private $name;
function __construct($name, $born) {
$this->name = $name;
$this->born = $born;
}
function getInfo() {
echo "My Friend $this->name was born in $this->born\n";
}
}
$friend = new Friend("Lenka", 1990);
$friend->getInfo();
?>
private $born;We have two variables in the class definition. The
private $name;
private
keyword is an access modifier. It is a form of encapsulation. The private
keyword is the most restrictive modifier. It allows only the object in question to access the variable. No descendants, no other objects. More about this topic later. function __construct($name, $born) {In the constructor, we initiate the two data members. The
$this->name = $name;
$this->born = $born;
}
$this
variable is a handler used to reference the object variables. $friend = new Friend("Lenka", 1990);We create a Friend object with two arguments. Then we call the getInfo() method of the object. To call object methods, we use the -> operator.
$friend->getInfo();
$ php friend.php
My Friend Lenka was born in 1990
Class constants
PHP enables to create class constants. These constants do not belong to a concrete object. They belong to the class. By convention, constants are written in uppercase letters.<?phpWe have a Math class with a PI constant.
class Math {
const PI = 3.14159265359;
public function getPI() {
echo self::PI;
}
}
$math = new Math();
echo Math::PI, "\n";
echo $math->getPI(), "\n";
?>
const PI = 3.14159265359;The
const
keyword is used to define a constant. public function getPI() {Class constants are accessed from within methods using the
echo self::PI;
}
self
keyword followed by two colons. echo Math::PI, "\n";We print the PI constant to the console. In the first case, we get the constant value by referring to the class name, followed by two colons and a constant name. Note that no object was needed to get the class constant. In the second case, we use the object method.
echo $math->getPI(), "\n";
The instanceof keyword
Theinstanceof
keyword is used to determine whether a PHP variable is an instantiated object of a certain class. <?phpIn the above script, we have three classes. Cat, Dog and Bird. We traverse the array and print the class for each array value.
class Cat {}
class Dog {}
class Bird {}
$objects = array(new Cat(), new Dog(), new Cat(),
new Bird(), new Bird(), new Dog(),
new Dog(), new Cat(), new Bird()
);
shuffle($objects);
foreach ($objects as $object) {
if ($object instanceof Cat) {
echo "It is a Cat\n";
} elseif ($object instanceof Dog) {
echo "It is a Dog\n";
} else if ($object instanceof Bird) {
echo "It is a Bird\n";
}
}
?>
$objects = array(new Cat(), new Dog(), new Cat(),We create an array of these objects.
new Bird(), new Bird(), new Dog(),
new Dog(), new Cat(), new Bird()
);
shuffle($objects);We shuffle the array. At this point, we don't know the class types for the values of the array.
if ($object instanceof Cat) {Here we use the
echo "It is a Cat\n";
}
instanceof
keyword to find out the type of the class. $ php instanceof.phpYou might get this output.
It is a Bird
It is a Cat
It is a Cat
It is a Dog
It is a Dog
It is a Cat
It is a Dog
It is a Bird
It is a Bird
The __toString() method
When we use theprint
or the echo
keyword with the object instance, the __toString()
special method is called. We will demonstrate this in the following example. <?phpWe have a Cat class with a
class Cat {
public $name;
public $age;
function __construct($name, $age) {
$this->age = $age;
$this->name = $name;
}
function __toString() {
return "Cat: $this->name, Age: $this->age \n";
}
}
$missy = new Cat("Missy", 6);
$lucky = new Cat("Lucky", 4);
print $missy;
echo $lucky;
?>
__toString()
special method defined. function __toString() {The method prints the basic info about the object.
return "Cat: $this->name, Age: $this->age \n";
}
$missy = new Cat("Missy", 6);We create two objects of the Cat class.
$lucky = new Cat("Lucky", 4);
print $missy;And we use the
echo $lucky;
print
or the echo
keywords on them. $ php tostring.phpThis is what we get, when we run the script.
Cat: Missy, Age: 6
Cat: Lucky, Age: 4
Inheritance
The inheritance is a way to form new classes using classes that have already been defined. The newly formed classes are called derived classes, the classes that we derive from are called base classes. Important benefits of inheritance are code reuse and reduction of complexity of a program. The derived classes (descendants) override or extend the functionality of base classes (ancestors).<?phpIn this PHP script, we have two classes. A Base class and a Derived class. The Derived class inherits from the Base class.
class Base {
function __construct() {
echo "Construction of Base class \n";
}
}
class Derived extends Base {
function __construct() {
parent::__construct();
echo "Construction of Derived class \n";
}
}
$obj1 = new Base();
$obj2 = new Derived();
?>
class Derived extends Base {In PHP, we use the
extends
keyword to create inheritance relations. function __construct() {In the constructor of the Derived class, we call the parent constructor. We use the
parent::__construct();
echo "Construction of Derived class \n";
}
parent
keyword, followed by two colons and the __construct()
method. The constructors of the parent classes must be called explicitly. $obj1 = new Base();We instantiate both the Base and the Derived classes.
$obj2 = new Derived();
$ php derived.phpThis is the output of the PHP script.
Construction of Base class
Construction of Base class
Construction of Derived class
A more complex example follows.
<?phpWe have used several new concepts here. In the code example, we have three classes. Being, Animal and Cat. The Animal class inherits from the Being class. The Cat class inherits from the Animal class. Classes inherit methods and data members, that are not declared as private.
abstract class Being {
protected $isAlive = true;
public function isAlive() {
if ($this->isAlive) {
echo "Being is alive\n";
} else {
echo "Being is not alive\n";
}
}
public function kill() {
$this->isAlive = false;
}
}
abstract class Animal extends Being {
protected $age;
public function __construct($age) {
$this->age = $age;
}
protected function setAge($age) {
$this->age = $age;
}
public function getAge() {
return $this->age;
}
}
class Cat extends Animal {
private $name;
public function __construct($name, $age) {
$this->name = $name;
parent::__construct($age);
}
public function getName() {
return $this->name;
}
}
$cat = new Cat("Cici", 4);
$cat->isAlive();
echo $cat->getName() . " is " . $cat->getAge() . " years old\n";
$cat->kill();
$cat->isAlive();
?>
abstract class Being {The Being class is declared to be
abstract
. The abstract keyword prohibits instantiation of classes. It does not make much sense to create an instance of the class Being. protected $isAlive = true;The $isAlive data member is declared to be
protected
. Such members can be accessed only by the classes, that define them and by their descendants. abstract class Animal extends Being {The Animal class is also declared to be abstract. It inherits from class Being. For this, we use the
extends
keyword. The Animal is descendant. It inherits methods and variables of the base Being class. class Cat extends Animal {The Cat class inherits from the Animal class. It inherits from the Animal class and indirectly from the Being class too. It is not declared abstract, which means, we can instantiate it.
parent::__construct($age);In the constructor of the Cat class, we call the parent constructor using the
parent
keyword, followed by two colons and the __construct()
method. The constructors of the parent classes must be called explicitly. $cat = new Cat("Cici", 4);We create a new Cat. Cici, 4 years old. Then we call functions on the cici object. Note the usage of methods, that were not created in the Cat class, but rather inherited from the parent classes.
$cat->isAlive();
echo $cat->getName() . " is " . $cat->getAge() . " years old\n";
$cat->kill();
$cat->isAlive();
$ php inheritance.phpOutput of the script.
Being is alive
Cici is 4 years old
Being is not alive
Abstract classes and methods
PHP 5 introduces abstract classes and methods. Abstract classes cannot be instantiated. If a class contains at least one abstract method, it must be declared abstract too. Abstract methods cannot be implemented, they merely declare the methods' signatures. When we inherit from an abstract class, all abstract methods must be implemented by the derived class. Furthermore, these methods must be declared with the same of less restricted visibility.Unlike interfaces, abstract classes may have methods with full implementation and may also have defined member fields. So abstract classes may provide a partial implementation. Programmers often put some common functionality into abstract classes. And these abstract classes are later subclassed to provide more specific implementation. For example, the Qt graphics library has a QAbstractButton, which is the abstract base class of button widgets, providing functionality common to buttons. Buttons Q3Button, QCheckBox, QPushButton, QRadioButton, and QToolButton inherit from this base abstract class.
Formally put, abstract classes are used to enforce a protocol. A protocol is a set of operations, which all implementing objects must support.
<?phpIn our PHP script, we have an abstract base Drawing class. The class defines two member fields, defines one method and declares one method. One of the methods is abstract, the other one is fully implemented. The Drawing class is abstract, because we cannot draw it. We can draw a circle, a dot or a square. The Drawing class has some common functionality to the objects, that we can draw.
abstract class Drawing {
protected $x = 0;
protected $y = 0;
public abstract function area();
public function getCoordinates() {
echo "\$x is $this->x\n";
echo "\$y is $this->y\n";
}
}
class Circle extends Drawing {
private $radius;
public function __construct($x, $y, $r) {
$this->radius = $r;
$this->x = $x;
$this->y = $y;
}
public function area() {
return $this->radius * $this->radius * pi();
}
public function __toString() {
return "Circle, at x: $this->x, y: $this->y, radius: $this->radius";
}
}
$o = new Circle(12, 45, 22);
echo "$o \n";
echo "Area of the circle: " . $o->area() . "\n";
echo $o->getCoordinates();
?>
class Circle extends Drawing {A Circle is a subclass of the Drawing class. It must implement the abstract area method.
$ php abstract.phpOutput of the script.
Circle, at x: 12, y: 45, radius: 22
Area of the circle: 1520.53084434
$x is 12
$y is 45
Interfaces
A remote control is an interface between the viewer and the TV. It is an interface to this electronic device. Diplomatic protocol guides all activities in the diplomatic field. Rules of the road are rules that motorists, cyclists and pedestrians must follow. Interfaces in programming are analoguos to the previous examples.Interfaces are:
- APIs
- Contracts
From the second point of view, interfaces are contracts. If agreed upon, they must be followed. They are used to design an architecture of an application. They help organize the code.
Interfaces are fully abstract types. They are declared using the
interface
keyword. Interfaces can only have method signatures and constants. All method signatures declared in an interface must be public. They cannot have fully implemented methods, nor member fields. A PHP class may implement any number of interfaces. An interface can also extend any number of interfaces. A class that implements an interface must implement all method signatures of an interface. Interfaces are used to simulate multiple inheritance. A PHP class can extend only one class. A PHP class can implement multiple interfaces. Multiple inheritance using the interfaces is not about inheriting methods and variables. It is about inheriting ideas or contracts, which are described by the interfaces.
There is one important distinction between interfaces and abstract classes. Abstract classes provide partial implementation for classes, that are related in the inheritance hierarchy. Interfaces on the other hand can be implemented by classes, that are not related to each other. For example, we have two buttons. A classic button and a round button. Both inherit from an abstract button class, that provides some common functionality to all buttons. Implementing classes are related, since all are buttons. Another example might have classes Database and SignIn. They are not related to each other. We can apply an ILoggable interface, that would force them to create a method to do logging.
<?phpThis is a simple PHP script demonstrating an interface.
interface IInfo {
public function do_inform();
}
class Some implements IInfo {
public function do_inform() {
echo "This is a Some class\n";
}
}
$sm = new Some();
$sm->do_inform();
?>
interface IInfo {This is an interface
public function do_inform();
}
IInfo
. It has the do_inform() method signature. class Some implements IInfo {We use the
implements
to implement from an interface. public function do_inform() {The class provides an implementation for the do_inform() method.
echo "This is a Some class\n";
}
The next example shows, how a class can implement multiple interfaces.
<?phpWe have a CellPhone class that inherits from three interfaces.
interface Device {
public function switch_on();
public function switch_off();
}
interface Volume {
public function volume_up();
public function volume_down();
}
interface Pluggable {
public function plug_in();
public function plug_off();
}
class CellPhone implements Device, Volume, Pluggable {
public function switch_on() { echo "Switching on\n"; }
public function switch_off() { echo "Switching off\n"; }
public function volume_up() { echo "Volume up\n"; }
public function volume_down() { echo "Volume down\n"; }
public function plug_in() { echo "Plugging in\n"; }
public function plug_off() { echo "Plugging off\n"; }
}
$o = new CellPhone();
$o->switch_on();
$o->volume_up();
$o->plug_in();
?>
class CellPhone implements Device, Volume, Pluggable {The class implements all three interfaces, which are divided by a comma. The CellPhone class must implement all method signatures from all three interfaces.
$ php interface.phpRunning the PHP script.
Switching on
Volume up
Plugging in
The next example shows how interfaces can extend from multiple other interfaces.
<?phpIn this PHP script, we define three interfaces. Extending interfaces allows us to organize them.
interface IInfo {
public function do_inform();
}
interface IVersion {
public function get_version();
}
interface ILog extends IInfo, IVersion {
public function do_log();
}
class DBConnect implements ILog {
public function do_inform() {
echo "This is a DBConnect class\n";
}
public function get_version() {
echo "Version 1.02\n";
}
public function do_log() {
echo "Logging\n";
}
public function connect() {
echo "Connecting to the database\n";
}
}
$db = new DBConnect();
$db->do_inform();
$db->get_version();
$db->do_log();
$db->connect();
?>
interface ILog extends IInfo, IVersion {The ILog interface extends other two interfaces.
public function do_log();
}
public function do_inform() {The DBConnect class implements the do_inform() method. This method was inherited by the ILog interface, which the class implements.
echo "This is a DBConnect class\n";
}
Polymorphism
The polymorphism is the process of using an operator or function in different ways for different data input. In practical terms, polymorphism means that if class B inherits from class A, it doesn’t have to inherit everything about class A; it can do some of the things that class A does differently. (wikipedia)In general, polymorphism is the ability to appear in different forms. Technically, it is the ability to redefine methods for derived classes. Polymorphism is concerned with the application of specific implementations to an interface or a more generic base class.
<?phpIn the above PHP script, we have an abstract Shape class. This class morphs into two descendant classes, Rectangle and Square. Both provide their own implementation of the area() method. Polymorphism brings flexibility and scalability to the OOP systems.
abstract class Shape {
private $x = 0;
private $y = 0;
public abstract function area();
}
class Rectangle extends Shape {
function __construct($x, $y) {
$this->x = $x;
$this->y = $y;
}
function area() {
return $this->x * $this->y;
}
}
class Square extends Shape {
function __construct($x) {
$this->x = $x;
}
function area() {
return $this->x * $this->x;
}
}
$shapes = array(
new Square(5),
new Rectangle(12, 4),
new Square(8)
);
foreach ($shapes as $shape) {
echo $shape->area() . "\n";
}
?>
This was the first part of the description of OOP in PHP 5.
0 comments:
Post a Comment