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 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 array_any: How it Works with Arrays with Examples

    PHP 8.4 released the array_any to test if at least one element matches a condition. Understand the array_any Function in…

    filter_has_var Function: Check if Input Exists in PHP

    User input does not always come through as expected. Sometimes values are missing or not set at all. This can…

    PHP File Inclusion: require, include, require_once, include_once

    PHP offers four main functions to include files: require, require_once, include, and include_once. Each one gives you a similar purpose…

    PHP OR Operator: Using || and or in Conditional Statements

    The PHP OR operator has two renditions: either || or or. Both of these are useful logical operators when you want to introduce…

    PHP mb_substr Function: How it Works with Examples

    You may need to get part of a text when you work with text in PHP. The mb_substr gives you…

    PHP array_change_key_case: Change Array Key Cases

    The array_change_key_case function in PHP changes all array keys to lowercase or uppercase for consistent data use. Understand the array_change_key_case…

    PHP Named Arguments vs. Positional Arguments

    The PHP named arguments are the names of the arguments through which the values are passed, allowing you to add…

    PHP array_intersect_ukey Function: How it Works with Examples

    The array_intersect_ukey is used to compare arrays by keys with a custom function in PHP. It returns matches by keys…

    PHP Integers Guide: Types, Limits, and Conversions

    You use integers (int) in PHP to count items, set page numbers, handle IDs, and manage loop counters. In this…

    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.