Blog de programación, errores, soluciones

Chose Language:
Author: Admin/Publisher |finished | checked

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

So 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.


To 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:

Modularidad: 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.


With 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.

A class is a template that defines a set of characteristics and behaviors for creating objects. It is like a mold that is used to create objects with the same properties and methods.

How to declare a class in PHP

In PHP, we can declare a class with the class keyword, and its syntax is as follows:

class Class_name{

The class name must start in uppercase

In the following example we are gonna made a Television class

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

Often, 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:

function __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:

The $this keyword$this_keyword

In 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

To 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->screen = "24 inches";
echo "<br>".$Televisior24->screen;


In 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

Sooner 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


Let’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();

Note: Interface methods must be public.

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.

Category: en-php
Something wrong? If you found an error or mistake in the content you can contact me on Twitter | @luisg2249_luis.
Last 4 post in same category