PHP Object | How to Create an Instance of a Class

The PHP object is an instance from the PHP class or the main data structure that is already been created by the PHP class. It can be assigned to a variable or called with $this that already refers to an individual object.

In another word, the PHP object is an instance from a kind of class, a thing like a human, animal or car. So each class can consist of two things.

  • Attributes
  • Actions

Entirely, everything you are seeing is an object from a class and each object can access the states or attributes and actions or behaviours of this class.

What is PHP Class

The PHP class can be defined by the defined word class it is already defining a new thing, So as you saw in the previous example. The car is a class, the people are a class and so on.

Let’s see where can we find the attributes and actions, In the next example we have a car and we need to specify what are the attributes and actions inside.

<?php 
   class car {
       
      // Attributes
      public string $type;
      public string $name;
      public int $speed;
      public float $model;

      // Actions      
      public function set_car_name( $name ='' ) {
        $this->name = $name;
      }

      public function change_speed( $speed = 25 ) {
        $this->speed = $speed;
      }
      
      public function set_model( $model = 53.544 ) {
        $this->model = $model;
      }
   }
?>

So in the previous example, we have defined a new class for a car. It contains attributes and actions. Whilst, The attributes are model, name, type and speed. And these can be changed by the actions according to the new data that have to be passed.

But we have a question about how to access an instance from this class to define a new type of Mercedes car.

What is an Object in PHP

As we mentioned before, it is an instance from a class that can specify the data structure in the class. Here we have to specify an object of the previous example.

<?php 
   $obj = new car();
?>

The new car(); is only one object from the car class so we can define unlimited counts from the class. Each one can be assigned to another variable.

To access the attributes and actions through the object. We write the assigned variable and beside it, a small arrow, after that the attributes or methods. Let’s see that in the following example.

<?php 
   // The below is an object ( instance ) from the main class
   $mercedes = new car();
   $mercedes->set_car_name( "Mercedes" );
   $mercedes->change_speed( 85 );
?>

By the way, we already created an object for the class it is a Mercedes car but how to print these values.

With the same object assigned to the $mercedes variable, we can print the new data.

<?php
   echo $mercedes->speed; // 85 
?>

Create a PHP Empty Object

PHP already has a default name for the class called with stdClass, the syntax can be like the below code.

<?php
   $empty = new stdClass();
?>

To fill the empty object with data, you have to use the arrow -> then add the value.

<?php
   $empty->property= "Value";
?>

Conclusion

PHP object is an instance from the class, there are unlimited instances from the class. Also, the word $this word refers to the object inside the class.

PHP has a default class, its name stdClass, we can use it to create and define an empty object. Follow PHP data types tutorial to learn more about other types.

    Similar Reads

    Understanding the PHP Exclusive OR (XOR) Operator

    In PHP, there is this interesting operator known as "exclusive OR," or just XOR. It is somewhat of an underdog…

    File Handling in PHP

    File Handling in PHP is an important tool especially when you need to manage files such as reading, writing, or…

    PHP array_is_list: Check if an Array is a List with Examples

    You need the array_is_list function to know if an array works as a list or not in PHP. What is…

    PHP Arrow Functions: Understanding “fn” Syntax

    Arrow functions were introduced in PHP 7.4. They offer you a way to write simple operations, such as calculations, filters,…

    Static Method in PHP: How They Work in Classes

    PHP static method lets you call functions without an object. In this article, we will cover the following topics: The…

    Abstract Class in PHP: How It Works & Examples

    Abstract class in PHP appeared to provide a way to define a structure for related classes and don't allow direct…

    PHP filter_var: Understand How to Sanitize Input

    PHP didn’t have a way to check or clean user input. Developers used scattered code—some wrote custom checks, others used…

    PHP echo vs print: Key Differences & Usage Guide

    Echo and print are foundational in displaying output in PHP, and though they might be similar in some ways, they…

    PHP MySQL ORDER BY: How to Sort Data in SQL?

    Sorting data is one of those things we do all the time but rarely stop to think about. Whether you…

    PHP MySQL Create Database: MySQLi & PDO Examples

    If you are working with PHP and MySQL, one of the first tasks you may need is to create a…

    Previous Article

    Master PHP Iterables: Arrays, Objects, Traversable Interface

    Next Article

    PHP Resource Type | How the get_resource_type() Works

    Write a Comment

    Leave a Comment

    Your email address will not be published. Required fields are marked *


    Subscribe to Get Updates

    Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
    No spam. Unsubscribe anytime.