How can you access a variable declared in one class from another class in PHP?

To access a variable declared in one class from another class in PHP, you can use the concept of inheritance. By extending the class that contains the variable, you can access it using the parent keyword. Alternatively, you can create an instance of the class that contains the variable and then access the variable through that instance.

class ClassA {
    public $variable = "Hello from ClassA";
}

class ClassB extends ClassA {
    public function displayVariable() {
        echo $this->variable;
    }
}

$classB = new ClassB();
$classB->displayVariable(); // Output: Hello from ClassA