How can global variables impact the assignment of class properties in PHP?

Global variables can impact the assignment of class properties in PHP by introducing unexpected behavior and making the code harder to maintain. To avoid this issue, it is recommended to avoid using global variables within classes and instead pass any necessary values as parameters to class methods or set them as properties within the class.

<?php
$globalVar = "Global Variable";

class MyClass {
    private $classProperty;

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

    public function getClassProperty() {
        return $this->classProperty;
    }
}

$instance = new MyClass($globalVar);
echo $instance->getClassProperty(); // Output: Global Variable
?>