Create a class in PHP

The PHP programming language is an object-oriented language, so one of the things we will have to learn is to create a class in PHP. And, create a class in PHP, it can be used to encapsulate a certain functionality, which we can instantiate with different values ​​in as many objects as we need.

Define a class in PHP

The first step we must take when creating a class in PHP is to define the class. The class name must be a generic name that represents the functionality we want to encapsulate within the class.

To define a class in PHP and thus be able to create a class in PHP we must use the following syntax in PHP:

class classname {
  // Class content
}

We use the class modifier followed by the name of the class we want to create. We insert the content of the class inside a block of keys.

In our case, we are going to declare a class that contains the information associated with a book, such as its title, the author, the publisher, the ISBN code,…

This is why we will define our book class in PHP as follows:

class Book {
  // Class content
}

Attributes of a PHP class

Next in our process of creating a class in PHP will work with the attributes. Once we have defined the class in PHP, we are going to add the attributes inside it. The attributes are the variables that will store the information within the PHP class.

In the PHP programming language, we can define attributes within a class using various visibility designations. These visibility designations, also known as access modifiers, determine where we can access attributes in our code. The three visibility designations that we can use are:

  • Public: Attributes defined as public are accessible from anywhere in the code, even outside the class in which they are defined.
  • Private: Attributes defined as private are only accessible within the class in which they are defined. They cannot be accessed from outside the class or from inherited classes.
  • Protected: Attributes defined as protected are accessible within the class in which they are defined and from the classes that inherit from it, but not from outside these classes.

It depends on the level of access we want to grant. A public attribute can be accessed from anywhere in the code, while a private attribute can only be accessed from within the class where it is defined.

In this way, a public attribute in PHP will be declared as follows:

public $attributeName;

A private attribute is defined as follows:

private $attributeName;

And finally, we will define a protected attribute using the following statement:

protected $attributeName;

Now, in our Book class we are going to define two private attributes using the private modifier that will allow us to store the title of the book and the author.

class Book {
    private $title;
    private $author;
}

PHP class constructor

Now, in the process of creating a class in PHP, we already have defined the class and the attributes that make up said class, let’s take the next step. This will be the creation of the the PHP class.

A the PHP class is the method that is invoked when we instantiate an object on said class. That is, it is the method that is invoked to construct the object and that will allow us to give initial values ​​to the attributes that we have just defined. Whenever this is necessary.

To be able to define a the PHP class we will have to create a public method named _construct. To do this, we will follow the following syntax:

public function _construct(){
  // Class constructor code
}

The constructor method can take parameters in case we need to instantiate the variables. So, in our case, if we want to initialize the title and author values, we can define the constructor of the Book class as follows:

class Book {

    private $title;
    private $author;

    public function __construct($title,$author) {
        $this->title = $title;
        $this->author = $author;
    }

}

In this part of the code we see that we are using the $this object. The $this object refers to the class itself and serves to reference the properties that we have defined previously.

So in the code:

$this->title = $title;

What we are saying is that the value of the $titulo parameter will be stored in the $titulo property which we reference using the $this object as $this->titulo.

Getters & Setters of a PHP class

Let’s follow the process of creating a class in PHP. We already have the class, the attributes and the constructor method that initializes the values. The next step is to define the methods or functions called getters & setters of a class in PHP. These functions are those that allow us, on the one hand, to obtain the value of a property, which would be the getters. And on the other hand, assign a value to a property, which would be the setters.

By having defined our properties as private using the private modifier we will have managed to maintain the integrity of the class and that is why we need the getters & setters.

In order to define a getter method we will simply have to define a function that returns the value of a property using the following syntax:

public function gettername() {
  return $this->property;
}

Therefore, to create a getter that returns the value of the title of a book, we will create the following getter:

public function getTitle() {
  return $this->title;
}

In the case of wanting to create a setter, what we will do is define a function that receives as a parameter the value of the property whose value we want to modify and in its code we will assign it to the property.

The syntax that a setter can have would be the following:

public function SetterName($property) {
  $this->property = $property;
}

This way we can create a setter for our book title using the following code:

public function setTitle($title) {
  $this->title = $title;
}

Integer definition to create a PHP class that defines a Book

Now we have defined our class enough to be able to instantiate it and start using objects on it.

If we take a look at what we have done to create a class in PHP we can see how our book class has turned out in PHP. We will see that the PHP code to create a PHP class will look like this:

class Book {

    private $title;
    private $author;

    public function __construct($title,$author) {
        $this->title = $title;
        $this->author = $author;
    }

    public function getTitle() {
        return $this->title;
    }

    public function getAuthor() {
        return $this->author;
    }

}

Instantiate a PHP class

Once the class is created, we will see that the next step is instantiate a class in PHP. That is, we are going to create an object that uses the class to be able to create an instance of said class. In our case of books, we would talk about instances such as the different books with their corresponding title and author that we want to create.

The syntax to instantiate an object a class in PHP will be the following:

$object = new ClassName(parameters)

So, if we return to our case, in this process of creating a class in PHP, from the Book class we will see that we can instantiate objects that contain books in the following way:

$l1 = new Book("Roma Soy Yo","Santiago Posteguillo");
$l2 = new Book("Alexandros","Valerio Massimo Manfredi");
$l3 = new Book("The Immortal Pyramid","Javier Sierra");

In this way we will have explained everything necessary to be able to create a PHP class and be able to instantiate objects on it. Tell us in comments what you think of the process to create a class in PHP and what things you would do that we have not explained.

Leave a Comment