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 array_intersect Function: How it Works with Examples

    The PHP array_intersect function finds common values in arrays and returns matches as a new array. Understand the array_intersect in…

    PHP Float: Understanding Float Precision in PHP

    A PHP float is a kind of number that has a decimal point, like 2.45 or 0.11. It’s also called…

    PHP file_exists: Check File and Directory Existence

    Whether you are just trying to keep your app from crashing or making sure your users’ uploads don’t accidentally overwrite…

    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 MySQL WHERE: How to Filter Data in MySQL

    Filtering data is a big part of getting the right information to show up. That is where the WHERE clause…

    PHP array_fill_keys: Create Arrays with Specific Keys

    The PHP array_fill_keys function helps you to build an array where each key has the same value. What is the…

    PHP substr Function: How to Extract and Manipulate Strings

    The substr() function in PHP returns part of a string. You give it a string, a starting point, and optionally…

    What Is PHP Used For? Meaning of PHP Programming Language

    When you first step into web development, one name seems to pop up everywhere: PHP. The PHP programming language has…

    PHP filter_var_array: How to Validate Multiple Inputs

    The filter_var_array() appeared to make input validation simple and sanitization in PHP. It handles multiple inputs with different filters was…

    PHP Filters: How to Validate and Sanitize User Inputs

    User input can be unpredictable and dangerous. A simple form submission might carry malicious code or invalid data and lead…

    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.