Initialization of objects in PHP

Sometimes you need to initialize an object - that is, to assign initial values to its properties. Suppose the name of the class is Coordinates and it contains two properties: the person's name and city of residence. You can write a method (function) that will initialize the object, for example Init():
// Creating a new class Coordinates: class Coordinates { // data (properties): var $name; var $city; // Method to initialize: function Init($name) { $this->name = $name; $this->city = 'Nottingham'; } } // Creating an object of the class Coordinates: $object = new Coordinates; // To initialize the object immediately call the method: $object->Init();
But don't forget to call the function right after creating the object, or call any method in-between creating the (operator new) object and its initialization (by calling Init).
In order for PHP to recognize which particular method should be automatically called when an object is created, it must be given the same name as the class (Coordinates):
function Coordinates ($name) $this->name = $name; $this->city = 'Nottingham'; }
The method that initializes the object is called constructor. However, PHP doesn't have destructors, because memory resources are freed automatically at the end of every script.