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.

 

Interfaces in PHP are used to define a contract or a blueprint for classes. They provide a way for developers to define a set of methods that a class must implement. The practical usages of interfaces in PHP are as follows:

Prerequisites:

 

Abstraction: Interfaces help in abstraction, which means they allow you to define a set of methods that a class must implement, without worrying about the implementation details.


Polymorphism: Interfaces in PHP allow you to create objects of different classes that implement the same interface. This makes it possible to create code that is more flexible and easier to maintain.


Dependency injection: Interfaces are often used in dependency injection to inject objects into a class. This allows you to write more flexible and reusable code.


Unit testing: Interfaces in PHP make it easier to write unit tests, as you can mock the behavior of the interface methods and test the class that implements the interface.


Code organization: Interfaces help in organizing your code by defining a clear set of methods that a class must implement. This makes it easier to understand the code and to maintain it over time.


API development: Interfaces are commonly used in API development to define the endpoints and methods that the API exposes. This helps in building a more modular and extensible API.


Overall, interfaces in PHP provide a way to define a contract between classes and make it possible to write more flexible, reusable, and maintainable code.

Modern PHP Enterprise Systems Applications require high efficient fast loading web sites. These are the key concepts you must configure in your Nginx Web Server to have a high performance web application:

- Enable caching: Nginx has a built-in caching mechanism that can significantly improve website performance. You can enable caching by adding the following lines to your Nginx configuration file:

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_cache_valid 200 60m;
proxy_cache_valid 404 1m;

- Use gzip compression: Nginx can compress responses before sending them to the client, which can greatly reduce the amount of data that needs to be transferred. You can enable gzip compression by adding the following lines to your configuration file:

gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

- Enable SSL: If you're not already using SSL, consider enabling it for improved security and search engine ranking along with parallel resource loading through HTTP2. You can enable SSL by adding the following lines to your configuration file:

listen 443 ssl;
ssl_certificate /path/to/certificate.crt;
ssl_certificate_key /path/to/private.key;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_ciphers HIGH:!aNULL:!MD5;

- Use FastCGI caching: If you're using PHP-FPM, you can enable FastCGI caching to cache the output of PHP scripts. This can greatly improve website performance. You can enable FastCGI caching by adding the following lines to your configuration file:

fastcgi_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_valid 200 60m;
fastcgi_cache_valid 404 1m;

- Optimize static file serving: Nginx can serve static files very efficiently, but you can further optimize this by adding the following lines to your configuration file:

Optimize static file serving: Nginx can serve static files very efficiently, but you can further optimize this by adding the following lines to your configuration file:

Refer to the sample config files here:

Nginx with PHP-FPM in Vagrant Centos 7 Virtual Machine

Install PHP7 with FPM in a Vagrant Centos 7

Configure PHP in Virtual Machine to work as LEMP Server

Configure Nginx Web Server with SSL in a Vagrant Virtual Machine

 

 

 

Zend OP Cache is key in having a very high performance Software Application running in PHP.

A good way to see how it is working, which files are cached and more, is using this very nice PHP Script.

Prerequisites:

 

copy ocp.php from my github repository to the folder where your virtual host configuration file is pointing for the index.php

$ nano /var/www/cool_project/public/ocp.php

 Click on the copy raw icon and paste the content of the file.

 

Save the file and if you set any of the virtual hosts configuration files posted here, you just need to open https://your.project.domain/ocp.php and you will see something like this:

 

Click on the Files button link and click on ungroup to get a list of all cached files and some useful stats: