Object-oriented programming in PHP: part 2

PHP Object-oriented programming part 2

In the previous part we saw what the object oriented paradigm is, what classes are and what objects are, we also saw that classes are made up of properties and methods. Quickly summarizing the object oriented paradigm or object oriented programming is a programming paradigm that allows the creation of objects that interact with each other independently from each other. Object-oriented programming is made up of classes and objects. Classes are the model and objects are the practical realization of classes. Classes are made up of properties and methods, properties are characteristics, and methods are the actions that can be performed on properties. Let’s take the example of a rectangle class in which you can have various properties such as the size of the base and the size of the height, the area and the perimeter, and to these properties you can apply various actions such as displaying or modifying the value of the base. and height, you can use these values ​​to calculate the area and perimeter.
Example of the realization of a class:

class Rectangle{

  /* Property */
  private $base;
  private $height;

  /* Constructor that contains the parameters to construct the object */ 
  public function __construct($base, $height)
  {

    /* I assign to the variables the values passed to construct the object */
    $this->base = $base;
    $this->height = $height;
  }

  /* Methods */

  /* I return the value of the base */
  public function getBase()
  {
    return $this->base;
  }

  /* I return the value of the height */
  public function getHeight()
  {
    return $this->height;
  }

  /* Calculate the area */
  public function getArea()
  {
    return $this->base * $this->height;
  }

  /* I calculate the perimeter */
  public function getPerimeter()
  {
    return (2 * $this->base) + (2 * $this-> height);
  }
   
}

To create an object, use the reserved word new in front of the class name

new Rectangle(20, 10);

Here is the Rectangle class instantiated. When you create an object it saves it in a variable so that you can create multiple instances of that class with possibly different properties.

rectangle1 = new Rectangle(5,15);
rectangle2 = new Rectangle(13,30);
rectangle3 = new Rectangle(25,18);

Here are created three objects of type Rectangle.
To create properties, you need to define a visibility indicators that can be public, private or protected followed by the property name and any value, the property name must be preceded by the $ symbol.

// Property example 
private $base;
public $height = 10;
protected $area;

To retrieve the value of a property, do the following.

$height = rectangle->height;

Visibility indicators

The visibility indicators are reserved words that have the purpose of defining the scope of visibility of a property or a method, they are three and are public, private and protected.
public: Members (properties or methods) declared public are accessible both from inside and outside the class and from classes derived from it;
private: can only be used within the class itself;
protected: can be used within the class itself and within derived classes, but are not accessible from outside the class.

The methods

Methods are simply functions belonging to classes designed to manage their actions.
Always taking the Rectangle class as an example, a method is defined as follows:

/* Calculate the area */
public function getArea()
{
  return $this->base * $this->height;
}

To define a method a visibility indicator is used (if the visibility indicator is omitted by default it is set to public), followed by the reserved word function and the name of the method, after the method name there goes a couple of brackets round where there may be any arguments inside, followed by one or more instructions enclosed in curly brackets called the body of the function.
To invoke the previous method, use the name of the object followed by a hyphen and a major / minor symbol (->) such as an arrow (In other languages the arrow is replaced by a period) and finally the name of the method concerned with a pair of round brackets where the necessary arguments will be placed inside.

echo rectangle1->getarea();
echo "<br>";
echo rectangle1->getarea(); 
echo "<br>";
echo rectangle1->getarea(); 
echo "<br>";

If the method does not require arguments, the round brackets must be left blank but must always be indicated.

The setter and getter methods

In PHP it is conventional to put the suffix set in front of a method name in case you want to change or define the value of a property.
Put the suffix get in front of the method name in case the method wants to show the value of a property. this system is not an obligation but a conventional rule to give an idea at first sight of the function of the method.

public function getBase()
{
  return $this->base;
}

public function setHeight($valueheight)
{
  $this->height = $valueHeight;
}

In this article we have seen how to define a method, we quickly recalled the visibility indicators and talked about the setter and getter methods, in the next we will explain the use of the arguments of a method, why they exist and what they are for.

 

SviluppoMania
Stay Tuned

MARCO.VERGNANI

Nella mia vita a 12 anni e' entrato a far parte un Intel 80286 con 4MB di RAM, un Hard disk da 20 MB e una primissima scheda VGA appena uscita e da allora mi si e' aperto un mondo pieno di bit. Appassionato di programmazione fin da piccolo, mi diverto a costruire piccoli robottini. Curioso delle molteplici applicazioni che le macchine automatiche possono compiere, e adoro vedere volare quegli strani oggetti chiamati droni.

Related Posts

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