<?php
class MyClass {
public $name;
// constructor method
public function __construct($name) {
$this->name = $name;
echo "Object created with name: " . $this->name . "<br>";
}
// destructor method
public function __destruct() {
echo "Object destroyed with name: " . $this->name . "<br>";
}
// custom method
public function myMethod() {
echo "Executing myMethod of object with name: " . $this->name . "<br>";
}
}
// create objects
$obj1 = new MyClass("Object 1");
$obj2 = new MyClass("Object 2");
// call a method on each object
$obj1->myMethod();
$obj2->myMethod();
// destroy objects (which calls destructors)
unset($obj1);
unset($obj2);
?>
MyClass
` has a constructor and a destructor. When objects of the class are created using the `new
` keyword, the constructor is called automatically, and when objects are destroyed (e.g. using `unset()
`), the destructor is called automatically.MyClass
` and calls a custom method on each object. Then the objects are destroyed using `unset()
`, which calls the destructor for each object. The output of the program should look like this :Object created with name: Object 1
Object created with name: Object 2
Executing myMethod of object with name: Object 1
Executing myMethod of object with name: Object 2
Object destroyed with name: Object 1
Object destroyed with name: Object 2