class MyClass { private $myProperty; public function __construct() { $this->myProperty = "hello"; } public function doSomething() { // do something } public function __destruct() { echo "Object destroyed"; } } $obj = new MyClass(); $obj->doSomething(); unset($obj); // calls __destruct() and frees memory
namespace MyPackage; class MyClass { // class definition } $obj = new MyClass(); $obj->doSomething(); // throws error - method does not exist unset($obj); // calls __destruct() and frees memoryIn this example, we create a new instance of the MyClass class which is part of the "MyPackage" namespace. We then call a non-existent method on the object, causing an error. Finally, we destroy the object using unset() and free up memory.