static
The static keyword has several distinct usages.
Static is one of the three special keywords that are used to access properties or methods from inside the class definition. It relates to the current called class.
Static is a keyword for methods and properties: they are turned into class elements, and should be reached with the :: syntax, instead of -> or ?->.
Static is a keyword for closures and arrow functions: it prevents the current scope from being included in the closure, thus limiting the number of accessible values.
Static is a keyword for variables: those variables aren’t removed at the end of the method execution and are available at the beginning of the next call.
<?php
class Elephpant {
private const HABITAT = 1;
function habitat() {
return static::HABITAT;
// same as \Elephpant::HABITAT; when the object is of class Elephpant
// same as \AsianElephpant::HABITAT; when the object is of class AsianElephpant
}
}
class AsianElephpant extends Elephpant {
private const HABITAT = 2;
}
?>