...

/

Creating Objects From a Class

Creating Objects From a Class

Learn how to create an object from a class and then get/set its properties.

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:

Press + to interact
<?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.

Press + to interact
<?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 ...