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

    PHP Assign Function to Variable – Examples and Explanation

    PHP variable function is a variable that is used to assign a function name with parentheses (). However, the PHP interpreter…

    PHP array_shift: Remove the First Array Element with Examples

    The array_shift function in PHP takes the first element from an array and returns it. It also moves all other…

    PHP array_column: How to Extract Values from Arrays

    The array_column function in PHP takes values from one column in a multidimensional array. You can use it to pull…

    PHP fread Function: How to Read Files in PHP

    In some cases, you need to handle a lot of data or simply try to open a file, or read…

    PHP Null Coalescing Operator: Handling Null Values

    The PHP null coalescing operator, written as ??, is a helpful feature added in PHP 7. It makes it easier…

    PHP $GLOBALS: Access Global Variables with Examples

    When you start working with PHP, you’ll soon need access to variables from multiple places in your code. For this,…

    Learn How to Insert Documents in MongoDB Using PHP

    Inserting documents into your MongoDB collections is one of the most basic but important tasks when working with PHP. In…

    PHP array_diff_key Function: How it Works with Examples

    PHP array_diff_key compares arrays and removes elements that share the same key. It checks only keys and keeps the values…

    Polymorphism in PHP: How It Works with Examples

    PHP 5 added support for classes and interfaces. This made object-oriented code possible with one feature being polymorphism. That means…

    PHP Predefined Constants Explained Simply

    PHP has a number of predefined constants that give you some information about the language and its environment. You can…

    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.