PHP Object
Last updated onThe 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.