What best practices should be followed when defining and using constructors in PHP classes?

When defining and using constructors in PHP classes, it is best practice to set default parameter values for optional parameters, use type declarations for parameters when possible, and initialize class properties within the constructor. This helps ensure that the class is properly initialized and reduces the chances of errors or unexpected behavior when creating instances of the class.

class MyClass {
    private $name;

    public function __construct(string $name = 'John Doe') {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

// Creating an instance of the class
$obj = new MyClass();
echo $obj->getName(); // Output: John Doe