PHP Classes & Objects: How to Create Classes and Objects in PHP

Here we are going to see How to Create Classes and Objects in PHP, What is $this keyword and PHP instanceof.

PHP Classes & Objects

We can Define classes as the abstract blueprint that we use to create objects.

While Classes are applied widely to objects that share the same attributes and method, the object is more specific.

For example, if are creating a class like Smartphone its object would be Samsung, Nokia, Oppo, Vivo, and so on.

All objects of a class have the same functions and instances, so it would be a brainer to create code for each one from scratch.

Even if an object is a little bit different from its type we can modify using concepts such as inheritance.

How to Define a Class

Similar to function where we define a function using funtion keyword, a class is defined by class keyword.

Here is the syntax of a class-

class ClassName{
    //Properties and Methods go here.
}

All class names should start with a capital letter.

Outside of class, we call Properties variable and Methods function but when they are inside classes they are called Properties and Methods in PHP.

Using the Following syntax here is a real-life example of a PHP Class

class Students{
   // Properties
   public $name;
   public $rollNumber;
  
   // Methods
   function set_name($name){
       $this->name = $name;
   }
    
   function get_name($name){
       return $this->name;
   } 
    
}

We create a class for Students and define two properties Name and Roll Number.

The first method inside the class is for set name and the second is forget name.

Using those methods we can define a student’s names and return when we needed in objects.

PHP $this Keyword explained

The $this Keyword only available inside the methods.

It refers to the current object inside methods.

When we create properties we generally do not assign their value, using the method we pave the way to define value inside or outside classes.

We create set or get the methods we use $this-> to refer to properties which value we wanna change letters.

class YourClassName{
    public $property;
        
  function set_property{
        $this->property = $property;
    }
    
   function get_property{
       return $this->property;
   }
}

How to Define an Object

Classes are created for the objects, without objects there is no need to create classes.

We already know that classes act like templates.

With objects, we can use methods and properties created in classes.

Here is the syntax for creating an object-

$objectName = new ClassName();

As you can see, to create a new object first we define the name of the object and use the new keyword and name of the class.

Since we already created class Students above here we are going to see how to create objects and define, their values-

# Creating new Objects
$ram = new Students();
$arjun = new Students();

# Setting values
$ram->set_name('Ram Singh');
$arjun->set_name('Arjun Yadav');

# Echoing Values

echo $ram->get_name();
echo $arjun->get_name();

PHP instanceof

In PHP instanceof is a keyword that returns a Boolean value of True or False.

This keyword is to confirm that the particular object belongs to a specific class.

Here is how we can use in our code-

var_dump($ram intanceof Students);

If that object is an instance of that particular class, True would be returned, otherwise, it would be false.