What best practices should be followed when defining methods in a PHP class to avoid dependencies on external factors not included in the method or class signature?

When defining methods in a PHP class, it's important to follow best practices to avoid dependencies on external factors not included in the method or class signature. One way to achieve this is by using dependency injection, where the dependencies are passed into the method or class constructor rather than being instantiated within the method itself. This helps to make the code more modular, testable, and less reliant on external factors.

class MyClass {
    private $dependency;

    public function __construct($dependency) {
        $this->dependency = $dependency;
    }

    public function myMethod() {
        // Use $this->dependency here
    }
}

$dependency = new MyDependency();
$myClass = new MyClass($dependency);
$myClass->myMethod();