You need to create a class before you create an object belonging to that class.
To create a class, you use PHP's class keyword.
Here's a really simple class:
class Truck {
}
This code defines a class called Truck that does nothing.
A class definition consists of the class keyword, followed by the name of the class.
To create an object, you use the new keyword, followed by the name of the class.
You can then assign this object to a variable, much like any other value.
<?php class Truck { // www. j a va2 s.c o m } $myTruck = new Truck(); $myTruck2 = new Truck(); print_r($myTruck); // Displays"Truck Object ()" print_r($myTruck2); // Displays"Truck Object ()" ?>
This code first defines the empty Truck class as before, then creates two new instances of the Truck class, two Truck objects.
It assigns one object to a variable called $myTruck, and another to a variable called $myTruck2.
Although both objects are based on the same class, they are independent of each other.
Each is stored in its own variable.
Objects are always passed by reference.