Can a class access the static property of another class and use it in a non-static method in PHP?
Yes, a class can access the static property of another class and use it in a non-static method in PHP. To do this, you can use the scope resolution operator (::) to access the static property of the other class within the non-static method. You can then use the value of the static property as needed in your non-static method.
class Class1 {
public static $staticProperty = "Hello from Class1";
}
class Class2 {
public function nonStaticMethod() {
$value = Class1::$staticProperty;
echo $value;
}
}
$class2 = new Class2();
$class2->nonStaticMethod(); // Output: Hello from Class1