Object-oriented programming in PHP: part 1

Programmazione ad oggetti in PHP: parte 1

From today we will introduce a mini-series of tutorials to describe object-oriented programming in PHP, the implementation of which was introduced as a strength of version 5, providing the ability to an already mature language such as PHP to be able to keep up with languages and more modern technologies, certainly giving a condition of greater reliability and maintainability, also allowing the possibility of solving problems of various kinds, even complex ones with relative ease.

General description

Object-oriented programming is a programming paradigm that in the modern scenario is among the most used in the world, composed of entities that interact with each other, which have a structure in their own right regardless of what may happen in the world around them. The main actors that make up the entities are the classes and the objects, the classes are the definitions of code blocks formed by properties and methods, the objects instead are the implementations of the definitions just mentioned.
When we think of classes we need to make a transmigration with the real world thinking as if they were a project or a model or a category of something, and the object is its physical realization. For example, the class can be compared to the project of a house and the object to its construction, from this we can say that by taking a project of a house we can build one, some, many. Another example can be a car that from the automobile class we can create many cars. Approaching the world of programming we can take the example of a button of a graphical interface that within the class we write all the code that composes it and then we can replicate it endless times, always based on the same model.

The classes

The classes as mentioned above are a model, a project of what will be an object, they are basically composed of two elements which are properties and methods.

Properties are the characteristics that a class can have, for example in a hypothetical car class the properties could be color, number of gears, speed, displacement. Another example we can do is of a person class which will have as its property the name, surname, age, height.

The methods, on the other hand, are the actions that can be performed, for example by taking up the case of the car we can display its speed, or display the kilometers traveled, or inflate its wheels.In the example of a person, its age can be changed every year. pass, or change its weight, view its height.

Now let’s see a short example:

<?php

/* I define a class */
class Person{

  private $name = "Carlo";
  private $surname = "Rossi";
  private $age = "59";
  
  public function getName()
  {
    return $this->name;
  }
  public function getSurname()
  {
    return $this->surname;
  }
  public function getAge()
  {
    return $this->age;
  }
}

/* I create the object */
$person = new Person();

echo $person->getName();
echo "<br>";
echo $person->getSurname();
echo "<br>";
echo $person->getAge();

For the moment let’s not worry about understanding the code that we will see in detail later.

The advantages

In object-oriented programming we have several advantages such as the reusability of the elements, this allows them to be reused as many times as we want in the same project or even in different projects if they allow their use. We also have a high degree of project maintainability, which means that by modifying a part of the program, the other elements that constitute it will not be affected by the effect, making it less rigid. We also have, using the object-oriented paradigm, the possibility of separating the elements of a project into many classes, having for each of them all the characteristics and functionalities that are needed for their correct existence, thus obtaining a more ordered code, which will make it more understandable and easier to maintain.

Create a class

Let’s see now how to use classes and objects.
To create a class, just type the keyword class followed by the name we want to give it, by convention the name is written with the first capital letter, the code that composes it must be written between the curly brackets as in the following example.

<?php
class MyClass{
 // The code that makes up the class
}

Now let’s see how to add properties to the class

<?php
class Book{
  public $title= "Don Chisciotte della Mancia";
  public $author= "Miguel de Cervantes";
  public $type= "novel";
  public $numberPages = "600";
  
}

The properties as seen in the previous example are defined as simple previous variables by the reserved words public, private, protected.
Now we can create the object and start using the properties defined in the class.

// I create the object by instantiating the Book class
$book= new Book();
// I see the properties
echo "Title: ". $book->title. "<br >";
echo "Author: " . $book->author. "<br >";
echo "Type: " . $book->type. "<br >";
echo "Number of pages: " .  $book->numberPages . "<br >";

As you can see in the previous code, to create an object from a class you need to initialize a variable defined by the reserved word new followed by the name of the class to instantiate.
To recall the properties we use the name of the object, then we write the hyphen followed by the major / minor symbol (->) and finally we put the name of the property to be displayed without $ preceding the name.
In this article we have introduced classes and objects, in the next we will start writing some methods and we will see what the reserved word “public” means that we put in front of the class properties.

 

SviluppoMania
StayTuned

MARCO.VERGNANI

Related Posts

This site uses Akismet to reduce spam. Learn how your comment data is processed.