Object-Oriented Programming in PHP
In the following article, we will see an introduction to object-oriented programming in PHP.
Article Content:
Introduction to OOP
- Object
- Advantages of using objects
- Classes
- How to declare a class in PHP
- Instantiate a class in PHP
- Visibility
- Class inheritance
- Interfaces
introduction to OOP
https://blastcoding.com/en/object-oriented-programming-in-php/#introSo that anyone who doesn’t know how to program and is looking at these definitions and how objects are declared and used, I will start with the simplest thing, which is to show what an object would be in the real world and what a class would be in it as well. This way, it will be easier to understand what objects and classes are in PHP or any other language.
Object
https://blastcoding.com/en/object-oriented-programming-in-php/#objectTo understand this, I ask you to look around your house, look at your house, and take an object, for example, the television. How is it composed? It has buttons, speakers, a screen, etc. We will call these fields or attributes.
In addition to this, it performs certain functions; we will call these methods.
In other words, we have 2 groups:
- The fields composed of buttons, speakers, screen, and others.
- The methods which are the functions that it performs, for example, turning the TV on or off, increasing the volume, etc.
Objects in software are similar to this way of thinking.They represent real-world entities, concepts, or abstractions within a software system.
Advantages of using objects:
https://blastcoding.com/en/object-oriented-programming-in-php/#advantagesModularidad: The source code of an object can be written and maintained independently of the code of other objects.
Encapsulation: When interacting with the methods of an object, the implementation that happens internally is hidden from the outside.
Reusability: If an object already exists, you can use it in your program.
Modifiability: If an object is causing problems, it can be easily replaced. This is similar to when something needs to be repaired and the part is simply replaced.
Classes
https://blastcoding.com/en/object-oriented-programming-in-php/#classesWith the Class imagine that we had the blueprints to build a television. That way, we could make any kind of television we wanted. Or we could also build houses, for example.
A class can be as elaborate as desired.
How to declare a class in PHP
https://blastcoding.com/en/object-oriented-programming-in-php/#how_to_declare_a_classIn PHP, we can declare a class with the class keyword, and its syntax is as follows:
Sintaxisclass Class_name{ }
In the following example we are gonna made a Television class
<?php class Televison{ //como mera introducción puse public private y protected public $screen; private $speakers; protected $buttons; function __construct(){ } function turnOn(){ echo "tv turn on"; } function turnOff(){ echo "tv turn off"; } } ?>
the method __construct
https://blastcoding.com/en/object-oriented-programming-in-php/#__constructOften, we need our object to perform a task when it is instantiated or to receive parameters. We can do this with the constructor; inside the class, the constructor is the __construct method.
For example, if we need to know if our object will always have attributes of a certain type, we can do the following:
constructorfunction __construct($p){ $this->screen = $p; }Unfortunately, PHP does not support method overloading in versions prior to 5.3, and therefore we cannot define 2 constructors, or any other 2 methods. But we can simulate it. You can see more about this topic in constructor overloading: https://desarrolloweb.com/articulos/sobrecarga-constructores-php.html.
The $this keyword
https://blastcoding.com/en/object-oriented-programming-in-php/#the_$this_keywordIn PHP, the $this keyword is used within class methods to refer to the current object to which the method belongs. $this is a reference to the object itself that is being worked on within the context of a class.
It allows you to access the properties and methods of the current instance of the class from within its own methods.
Instantiate the class
https://blastcoding.com/en/object-oriented-programming-in-php/#nstantiate_the_classTo see how to instantiate, we will add the following code to the class we created earlier. Note that in the following code we use the new keyword to create an instance of the Television class.
$Televisior24 = new Televison(); $Televisior24->turnOn(); $Televisior24->screen = "24 inches"; echo "<br>".$Televisior24->screen;
Visibility
https://blastcoding.com/en/object-oriented-programming-in-php/#visibilityIn both PHP and other languages, we can define the visibility of properties and methods using the keywords: public, private, and protected.
public
: All methods and properties with the public keyword are accessible within the project.
protected
: All methods and properties with the protected keyword will be accessible, both within the class and in subclasses.
private
: All methods and properties with the private keyword will only be accessible within the class.
Inheritance in PHP Classes
https://blastcoding.com/en/object-oriented-programming-in-php/#inheritanceSooner or later we realize that an object is part of a larger group. To put ourselves in context, think of it this way: a car and a truck have things in common, both are vehicles. If we think about it, all vehicles have lights, wheels, windshield, horn, etc.
So it would be good that every time we make a vehicle that has these attributes and some behaviors that occur in different vehicles, we don’t have to repeat them.
class Vehicle{ private $lights; private $wheels; private $doors; public function start(){ //code to start the vehicle } public function shutdown(){ // code to shutdown the vehicle } }
In PHP, we can inherit the attributes and methods of the Vehicle class using the extends keyword. We can also say that the Vehicle class is the parent class and the Car class is the child class.
class Car extends Vehicle{ public function functionality(){ //code functionality } }
Interfaces
https://blastcoding.com/en/object-oriented-programming-in-php/#interfacesLet’s take into account that there are situations in which we already know the behavior that the object will have. In this way, we can already predefine which methods should be implemented in certain classes. And since we were talking about vehicles, the following example of how to create an interface will be for a vehicle.
By convention, interfaces have the letter i in front.
interface iVehicle { public function start(); public function shutdown(); }
This is how an interface is defined in PHP; we define the methods and whether they have parameters or not.
To be able to use an interface in a class, the keyword “implements” must be used.
class Vehicle implements iVehicle{ private $lights; private $wheels; private $doors; public function start(){ //code to start the car } public function shutdown(){ //code to shutdown the car } }
Please note that the functions “start” and “shutdown” must be defined in the class since we are using the interface iVehicle.