PHP Object-Oriented Programming Beginner's Guide
Basic concepts around Object-Oriented Programming
Class vs Object
Class is like your house blueprint. Before your house is constructed, there is a house blueprint. It is not an actual house, but a plan how this house will look like, how many rooms it will have and so on. Then the house will be constructed by following the blueprint exactly. In this analogy, the house blueprint is a class and your actual house is an object. We can have unlimited objects of a class, just like we can build unlimited exact houses by following the same house blueprint.
A few key points to keep in mind:
- Class is generic, whereas Object is specific
- Class defines properties/functions of an Object
- Object is an instance of a Class
- You can instantiate an object, but not a Class
PHP Class
Class is consist of properties and methods. Below is a PHP class. In this simple class. $postCode is a property and ringBell() is a method. They are all prefixed with a visibility keyword (public).
Class House { public $postCode = “560121”; public function ringBell() { echo “Ding Dang Dong”; } }
To instantiate an object of a class, use the keyword new as below:
$house = new House();
Visibility
Each method and property has its visibility. There are three types of visibility in PHP. They are declared by keywords public, protected and private. Each one of them controls how a method/property can be accessed by outsiders.
Public: It allows anyone from outside access its method/property. This is the default visibility in PHP class when no keywords are prefixed to a method/property.
Protected: It only allows itself or children classes to access its method/property.
Private: It does not allow anyone except itself to access its method/property.
Inheritance
It lets subclass inherits characteristics of the parent class. Parent class decides what and how its properties/methods to be inherited by declared visibility.
class Shape { public function name() { echo "I am a shape"; } } class Circle extends Shape { } $circle = new Circle(); $circle->name(); // I am a shape
The key word here is extends. When Circle class extends from Shapeclass, it inherits all of the public and protected methods as well as properties from Shape class.
Polymorphism
The provision of a single interface to entities of different types. Basically it means PHP is able to process objects differently depending on their data type or class. This powerful feature allows you to write interchangeable objects that sharing the same interface.
interface Shape { public function name(); } class Circle implements Shape { public function name() { echo "I am a circle"; } } class Triangle implements Shape { public function name() { echo "I am a triangle"; } } function test(Shape $shape) { $shape->name(); } test(new Circle()); // I am a circle test(new Triangle()); // I am a triangle
Above example, test(Shape $shape) function declares(type hints) its only parameter to be Shape type. This function is not aware of Circle and Triangle classes. When either class is passed to this function as a parameter, it processes respectively.
Encapsulation
Encapsulation is used to hide the values or state of a structured data object inside a class, preventing unauthorized parties' direct access to them. It is a concept that motivates us to think through a method/class responsibility and hide its internal implementation/details accordingly. This will make it easy to modify the internal code in a long run without affecting other part of the system. Visibility is the mechanism for encapsulation.
class Person { private $name; public function setName($name) { $this->name = $name; } public function getName($name) { return $this->name; } } $robin = new Person(); $robin->setName('Robin'); $robin->getName();
In this simple class above. Field $name is encapsulated (private). Users of the class is not aware how $name is stored in Person class. Right now the $name is stored in memory. We can modify internal code of Person class to store it to a flat file or event a database. Users of the class will not need to change any code, in fact they do not even know how $name is stored, because that is encapsulated and hided from them.
Abstraction
Abstraction is the concept of moving the focus from the details and concrete implementation of things, to the types of things (i.e. classes), the operations available (i.e. methods), etc, thus making the programming simpler, more general, and more abstract. It is like a generalization instead of a specification.
class TV { private $isOn = false; public function turnOn() { $this->isOn = true; } public function turnOff() { $this->isOn = false; } } $tv = new TV(); $tv->turnOn(); $tv->turnOff();
Code above defined an TV class. We can not do much with it except turning it on and off. The class TV is an abstraction of a real TV in a very simple use case.
Interface vs Abstract class
Interface
Interface declares what methods a class must have without having to implement them. Any class that implements the interface will have to implement details of those declared methods. Interface is not a class, so you can not instantiate an interface. It is useful when you need to enforce some classes to do something.
interface Vehicle { public function startEngine(); } class Car implements Vehicle { public function startEngine() { echo "Engine Started"; } }
Vehicle is an interface with a declared method startEngine(). Carimplements Vechicle, so it has to implement what startEngine() method does.
Abstract class
Abstract class is able to enforce subclasses to implement methods similar to interface. When a method is declared as abstract in an abstract class, its derived classes must implement that method.
However it is very different from interface. You can have normal properties and methods as a normal class, because it is in fact a class, so it can be instantiated as a normal class.
abstract class Vehicle { abstract public function startEngine(); public function stopEngine() { echo "Engine stoped"; } } class Car extends Vehicle { public function startEngine() { echo "Engine Started"; } }
Vehicle is an abstract class. Car extends Vechicle, so it has to implement what startEngine() method does, because this method is declared as abstract. However Car does not have to anything with method stopEngine(), it is inherited as a normal class does.
0 comments:
Post a Comment
Thanks