How does the 'new' keyword work in PHP when creating instances of classes?
When creating instances of classes in PHP, the 'new' keyword is used to instantiate a new object of a specific class. This keyword is followed by the class name and optional parentheses if the class has a constructor that needs arguments. The 'new' keyword allocates memory for the object and calls the class constructor to initialize the object.
class MyClass {
public function __construct($param) {
echo "Constructor called with parameter: $param";
}
}
// Instantiating an object of MyClass
$obj = new MyClass("Hello");