A brief Introduction to Object-oriented programming in PHP

This tutorial will teach the basic concepts of object-oriented programming that can be leveraged to write real-time modular apps.

There are many benefits of using an object-oriented approach over the traditional procedural way of writing code, but the most significant advantages are secure data, reusability, and easy debugging.

Procedurally, lots of data is classified as global data and becomes vulnerable when get approached by several functions. In the object-oriented approach, data is secure within classes that external functions can’t modify.

An object represents a real-life entity with a certain number of actions. For example, a dog can be described using its color, breed, age, and so on, and performs specific actions such as barking, running, wagging his tail, jumping, etc. In OOP, data and functionality are bundled together into an entity known as objects.

Different objects can interact with each other. For example, in a library management system, student objects can interact with the book object. Students can borrow, read, or return the book.

The difference between classes and objects

A class is a blueprint for creating an object. You can create unlimited objects from a single class. Like you can create an unlimited number of houses from a blueprint.

In PHP, the class begins with the class keyword followed by the name of a class and the body enclosed in curly braces.

class ClassName {

//variables
//methods
}

A class name starts with a letter or underscore followed by any number of letters, numbers, or underscore. Just like a variable name, the class name can’t begin with a number.

PHP Standards Recommendations

[adinserter block=”2″] PSR-1 recommends that a class name is declared in CamelizedClassName, and class methods are declared in camelizedFunctionName.

You can learn more about the coding standards in PHP – here. (https://www.php-fig.org/psr/psr-1/)

Let’s create a simple class – Person.

class Person {
    public $name = 'John Doe';

    function sayHello() {
        echo 'Hello!';
    }
}

How to instantiate a class in PHP?

An object is an instance of a class. To create an object in PHP, you need to instantiate a class using the new keyword.

$obj = new Person();

Here the $obj doesn’t contain the object; instead, it points to the object.

Class constructor (__construct) and destructor (__destruct) are two special kinds of methods that automatically are called when objects are created or destroyed, respectively.

To access an object’s property or method, you can use the -> operator, as follows:

$obj - > property;
$obj -> method();

In PHP, class members can be public, private, or protected. These keywords define the class member’s visibility across the code.

Static properties can be written using the static keyword and can be accessed using the self keyword following by:: operator (scope operator).

Values that are not supposed to change within the class should be declared with the const keyword.

For example,

    const NAME = 'John Doe';

    function printName() {
        echo self::NAME;
    }
}
echo AClass::NAME;

The $this variable

[adinserter block=”2″] $this is a pseudo-variable. It is used to access object members.

class Person {
    public $name;

    function getName() {
        return $this - > name;
    }

    function setName() {
        $this - > name = 'John Doe';
    }
}

For the practice, create a class book containing attributes such as book title, author, subject, isbn_number, and create two objects from these.

I hope this tutorial made you understand the basics of object-oriented concepts in PHP.