PHP is a Computer Language that embraced OOP (Object Oriented Programming) and currently it fully supports all OOP Principles and Patterns. This makes PHP a very powerful, yet friendly Programming Language. Mastering OOP in PHP requires to fully understand the concept of Objects.

Prerequisites:

An Object is made of 2 things:

  • Properties. These ones store data of any type like Integers, Floating Point Numbers, Strings, Arrays or even another Objects.
  • Methods. These ones are code snippets encapsulated in a function that performs a given task most of the time interacting with the object properties. 

 

In PHP the Object definition is called a Class:

<?php

namespace App\Entity;

/**
 * @package Zoo Animals
 */
class Animal
{
    /**
     * @var string|null
     */
    private ?string $species;
    
    /**
     * @var int|null
     */
    private ?int $eyes;
    
    /**
     * @var int|null
     */
    private ?int $legs;
    
    /**
     * @var float|null
     */
    private ?float $tail;
    
    /**
     * @return string|null
     */
    public function getSpecies(): ?string
    {
        return $this->species;
    }
    
    /**
     * @param string|null $species
     */
    public function setSpecies(?string $species): void
    {
        $this->species = $species;
    }
    
    /**
     * @return int|null
     */
    public function getEyes(): ?int
    {
        return $this->eyes;
    }
    
    /**
     * @param int|null $eyes
     */
    public function setEyes(?int $eyes): void
    {
        $this->eyes = $eyes;
    }
    
    /**
     * @return int|null
     */
    public function getLegs(): ?int
    {
        return $this->legs;
    }
    
    /**
     * @param int|null $legs
     */
    public function setLegs(?int $legs): void
    {
        $this->legs = $legs;
    }
    
    /**
     * @return float|null
     */
    public function getTail(): ?float
    {
        return $this->tail;
    }
    
    /**
     * @param float|null $tail
     */
    public function setTail(?float $tail): void
    {
        $this->tail = $tail;
    }
    
    /**
     * @return float|null
     */
    public function getMotionSpeed(): ?float
    {
        return $this->motionSpeed;
    }
    
    /**
     * @param float|null $motionSpeed
     */
    public function setMotionSpeed(?float $motionSpeed): void
    {
        $this->motionSpeed = $motionSpeed;
    }
    
    /**
     * @var float|null
     */
    private ?float $motionSpeed;
    
}

 

Most of the time we use to add a special method to the class definition called Constructor, that we use to initialize certain properties:

 

    /**
     * @param string|null $species
     */
    public function __construct(?string $species)
    {
        $this->species = $species;
    }

 

Once we have the object class definition we can create as many object instances as we want from this doing:

 

//Zoo Animals
$zebra = new Animal('Zebra');
$lion = new Animal('Lion');
$elephant = new Animal('Elephant');


echo '<pre>';
echo '<strong>Current Animal Species in our Zoo:</strong><br/><br/>';
echo '<ul>';
echo '<li> 1.-  ' . $zebra->getSpecies() . '</li>';
echo '<li> 2.-  ' . $lion->getSpecies() . '</li>';
echo '<li> 3.-  ' . $elephant->getSpecies() . '</li>';
echo '</ul>';
echo '</pre>';

 In the Browser we see this:

 PHP uses the Class definition as a template to create each one of the animal instances and assign it to each one of the variables ($zebra, $lion, $elephant).

Notice that for each Animal, we passed along with the new Animal(..) Instruction, the Species Name of each one as a string parameter. This string parameter is passed to the Constructor method at the moment of object instantiation.

Using the Constructor method is a very common way to set the initial values of the object properties.

It is very important to understand that the data type passed to the Constructor Method as a parameter, need to be of the type declared, otherwise a Type Exception will be thrown:

 

If we do this:

//Zoo Animals
$zebra = new Animal([15.6]);

we got this exception thrown by PHP:

Fatal error: Uncaught TypeError: App\Entity\Animal::__construct(): Argument #1 ($species) must be of type ?string, array given, called in /var/www/cool_project/php_code/src/index.php on line 11 and defined in /var/www/cool_project/php_code/src/Entity/Animal.php:33 Stack trace: #0 /var/www/cool_project/php_code/src/index.php(11): App\Entity\Animal->__construct() #1 {main} thrown in /var/www/cool_project/php_code/src/Entity/Animal.php on line 33

 Type casting is one of the most important concepts to keep in mind when building modern applications with PHP. It is very powerful tool that allows us to have a very robust code base.