Search⌘ K
AI Features

Creating Objects From a Class

Explore creating objects from a class in PHP by using the new keyword for instantiation. Understand how objects share class methods but can have individual property values. Learn to set and get properties for objects, enabling you to manage object data effectively.

How to create an object from a class

In order to work with a class, we need to create an object of it. In order to create an object, we use the new keyword. For example:

PHP
<?php
class Car {
// The code
}
$bmw = new Car ();
?>
  • In line 5, we create the object $bmw of class Car with the help of the new keyword.
  • The process of creating an object is also known as instantiation.

We can instantiate several objects of the same class, with each object having its own set of properties.

PHP
<?php
class Car {
// The code
}
$bmw = new Car ();
$mercedes = new Car ();
?>

In fact, we can instantiate as many objects as we like of the same class and then give each object its own set of properties.

The function of objects

In the procedural style of programming, all of the functions and variables sit together in the global scope in a way that allows their use just by calling their name.

Using classes hides anything inside from the global scope. That’s because the code inside the classes is encapsulated within the class scope, which is outside the reach of the global scope. As a result, we need a way to allow the code from the global scope to use the code within the class. We do this by creating objects from a class.

All objects that we instantiate will share the class’s methods and properties. Let’s look at the image below to understand this concept better.

We create three individual objects within the same Car class with the names “Mercedes,” “Bmw,” and “Audi.” Although all of the objects were created of the same class and thus have the class’s methods and properties, they are still different. This is not only because they have different names, but also because they may have different values assigned to their properties. For example, in the image above, they differ by the color property—the Mercedes is green while the Bmw is blue and the Audi is orange.

Note: A class holds the methods and properties that are shared by all of the objects that are created from it. Although the objects share the same code, they can behave differently because they can have different values assigned to them.

How to get an object’s property

Once we create an object, we can get its properties. For example:

PHP
<?php
class Car {
public $comp;
public $color = "beige";
public $hasSunRoof = true;
}
// Creating objects from class
$bmw = new Car ();
$mercedes = new Car ();
// Getting object's property
echo "The BMW car color is " . $bmw -> color;
echo "\n";
echo "The Mercedes car color is " . $mercedes -> color;
?>
  • In order to get a property, we write the object name, then a dash and the greater-than symbol (->), and finally the property name.
  • In lines 11 and 13, we get the color property of $bmw and $mercedes objects of the Car class.

Note: The property name does not start with the sign. Only the object name starts with a .

How to set an object’s property

In order to set an object property, we use a similar approach.

For example, in order to set the color to blue in the $bmw object, we write the following line:

$bmw -> color = 'blue';

Once we set the value of a property, we can get its value. In order to get the color of the $bmw, we use the following line of code: echo $bmw->color;

PHP
<?php
class Car {
public $comp;
public $color = "beige";
public $hasSunRoof = true;
}
// Creating objects from class
$bmw = new Car ();
$mercedes = new Car ();
// setting color property of bmw object
$bmw -> color = 'blue';
// Displaying property
echo "The BMW car color is " . $bmw->color;
?>

In the code above, we create a $bmw object of the Car class. In line 11, we set the color property of an object to blue. In line 13, we display the color property of the $bmw object.

In order to set the value of the comp property for both objects:

$bmw -> comp = "BMW";
$mercedes -> comp = "Mercedes Benz";

We can also get the color and the comp of the second car object.

echo $mercedes -> color;
echo $mercedes -> comp;

Let’s try this in the code editor:

PHP
<?php
class Car {
public $comp;
public $color = "beige";
public $hasSunRoof = true;
}
// Creating objects from class
$bmw = new Car ();
$mercedes = new Car ();
// Setting comp property of objects
$bmw -> comp = "BMW";
$mercedes -> comp = "Mercedes Benz";
// Displaying second object's color and comp properties
echo "The car color is " . $mercedes -> color;
echo "\n";
echo "The car company is " . $mercedes -> comp;
?>

In the code above, we create two objects, $bmw and $mercedes of the Car class, and set the comp property for both accordingly. In line 14, we display the color property and in line 16, we display the comp property for the $mercedes object only.