How does static keyword in PHP OOP help in accessing parent class variables?
When accessing parent class variables in PHP OOP, the static keyword can be used to access these variables without needing an instance of the parent class. This is useful when you want to access variables or methods from a parent class without creating an instance of it.
class ParentClass {
public static $parentVar = "I am a parent variable";
}
class ChildClass extends ParentClass {
public function getParentVar() {
return parent::$parentVar;
}
}
$child = new ChildClass();
echo $child->getParentVar(); // Output: I am a parent variable
Keywords
Related Questions
- What are the advantages and disadvantages of using PHP Core Modules like LibXML for tasks involving HTML manipulation?
- What are some recommended libraries or resources for extracting EPG data from a TV card in PHP?
- What are the security implications of running PHP scripts locally without a web server?