Signup/Sign In

Class in PHP (OOP)

PHP being an object oriented programming language, allows creation of classes and objects to follow the object oriented programming paradigm while developing any software or application.

A class is a user-defined data type which includes local variables and local methods.

While an object is an instance of the class which holds the local variables with values assigned and on which we can call the local methods defined in the class.

In this tutorial, we will learn how to define a class in PHP and how to create its objects.


Defining a Class in PHP

When we define a class in PHP, just like any variable we have to give our class a name, and there are some rules that we must follow while naming our class in PHP, they are:

  • The class name should start with an alphabet.
  • The class name should not be a reserved PHP keyword.
  • The class name should not contain any spaces.

Now that we know the rules related to naming convention for classes in PHP, let's create a simple class with name Person.


Syntax for defining a Class in PHP

The syntax for defining a class in PHP is very simple, we use the keyword class followed by the name of the class and then we enclose the code for the class within curly braces { } just like for a function/method.

Below is the code for class Person:

<?php
    class Person {
    
        // class variables and methods comes here...
        
    }
?>

Although it is not mandatory, but it is good practice to use the class name as the filename for the PHP file. Also, PHP allows for multiple classes to be defined in a single file, but again it is not recommended.

So we will save the above code for the class Person in the file Person.php.

Now let's add a variable to our class to store name of a person. When we create a variable inside a class, it is called property.

<?php
    class Person {
        // to store name of person
        var $name;
        
    }
?>

var is the keyword to define a variable/property inside a class and $name is the name of the variable.

It's time to add methods/functions inside our class. Functions, when defined inside a class are called methods.

Inside a class, methods are defined to either perform some operation on the class variables(properties) or perform some other operation for the class, like print the values of all the variables, or may be store data into database etc.

When we define methods to get the value of a class variable and to set value of a class variable, these methods are called Getter and Setter functions.

These are popularly named as get_NAME-OF-VARIABLE() and set_NAME-OF-VARIABLE().

So, for the variable $name, let's add get_name() and set_name() functions.

<?php
    class Person {
        // to store name of person
        var $name;
        
        // print name
        function get_name() {
            return $this->name;
        }
        
        function set_name($new_name) {
            $this->name = $new_name;
        }
        
    }
?>

We will talk more about why we need getter and setter functions, when we will learn how to create objects of class.


Can we define a Class without any Variable?

Yes, a simple class dowsn't require any variables. We can simply have some methods in it. Such classes are generally helper class with some useful common methods in them.

Let'screate a simple class with name Human with two methods:

<?php
    class Human {
        
        // for male
        function male() {
            echo "This human is a male";
        }
        
        function female() {
            echo "This human is a female";
        }
        
    }
?>

For the above class, all the objects will be similar as there is no variable.

When we have variables in a class, we can create multiple objects of that class with different values for the class variables.

So by now, we hope you have understood what a class is, its nothing but a user defined data type, which has properties/variables and class methods. In the next tutorial we will learn about how to create class objects.