MENU
The static Keyword and Constants
A constant, value of which does not change, can be declared for a class. It can be overridden in the child class. A static property or method can be accessed without any instantiation of the class. While a class constant can only be public, a static property can be protected or private. A constant or a static member can be accessed with ::.Only constants and static members may be accessed without any instantiation. They must be accessed with ::, with the exeception of
$obj->static_func(). parent:: and self:: refer to the parent class and current class respectively. $this must not be used in a static method.
An object can be created out of a static function.
$obj->static_func(). parent:: and self:: refer to the parent class and current class respectively. $this must not be used in a static method.
<!DOCTYPE html>
<html>
<head></head>
<body><?php
class P{
const c=1;
static private $v1=2;
static $v2=3;
public $v3=4;
static function f(){
echo self::$v1;
//echo $this->v3=1; //error
}
function g(){
echo 5;
}
}
class C extends P{
static function f(){ // method overriding
echo parent::c;
echo self::c; // also visible
}
}
echo P::c; //1
echo P::$v2; //3
P::f(); //2
C::f(); //11
$s="P";$s::f(); //2
$s="f";P::$s(); //2
$a=new P;
echo $a::c; //1
echo $a::$v2; //3
$a::f(); //2
$a->f(); //2
//P::g(); // 5, warning
//$a::g(); // 5, warning
//echo $a->c; // error
//echo $a->$v2;// error
//echo $a::$v3; // error
//echo P::$v3; // error
//P->f(); // error
//P->c1; // error
?></body>
</html>13211221322
Only constants and static members may be accessed without any instantiation. They must be accessed with ::, with the exeception of
$obj->static_func(). parent:: and self:: refer to the parent class and current class respectively. $this must not be used in a static method.
Late static binding can be achieved with static::. It allows an overriding member to be chosen, when the member is used from the base class.
$obj->static_func(). parent:: and self:: refer to the parent class and current class respectively. $this must not be used in a static method.
<!DOCTYPE html>
<html>
<head></head>
<body><?php
class C{
static function getNew(){
return new static;
}
}
$a=C::getNew();
var_dump($a instanceof C);
?></body>
</html>bool(true)
Only constants and static members may be accessed without any instantiation. They must be accessed with ::, with the exeception of
$obj->static_func(). parent:: and self:: refer to the parent class and current class respectively. $this must not be used in a static method.
Since PHP 5.6, a class constant's value may be a constant scalar expression – an arithmetic or string expression built from literals and other constants – instead of only a bare literal. The expression is evaluated once, when the constant is defined, and may reference constants declared earlier in the same class via self:::
$obj->static_func(). parent:: and self:: refer to the parent class and current class respectively. $this must not be used in a static method.
<!DOCTYPE html>
<html>
<head></head>
<body><?php
class P1 {
public static function which() {
echo __CLASS__;
}
public static function test() {
self::which(); //P1
static::which(); //C1
P1::which(); //P1
C1::which(); //C1
}
}
class C1 extends P1 {
public static function which() { // overriding method
echo __CLASS__;
}
}
C1::test();
class P2 {
private function which() {
echo __CLASS__;
}
public function test() {
$this->which(); //P2
self::which(); //C2
}
}
class C2 extends P2 {
public function which() {
echo __CLASS__;
}
}
$v2=new C2;
$v2->test();
?></body>
</html>P1C1P1C1P2P2
<?php
const ONE = 1;
const TWO = ONE * 2;
class C {
const THREE = TWO + 1;
const ONE_THIRD = ONE / self::THREE;
const SENTENCE = 'The value of THREE is '.self::THREE;
public function f($a = ONE + self::THREE) {
return $a;
}
}
echo (new C)->f()."\n";
echo C::SENTENCE;
?>4
The value of THREE is 3
<?php
class Config {
public const NAME = "MyApp";
protected const SECRET = "shh";
private const INTERNAL = "hidden";
public function reveal() {
return self::SECRET . " / " . self::INTERNAL;
}
}
echo Config::NAME . "\n";
echo (new Config())->reveal() . "\n";
?>MyApp
shh / hidden
<?php
class Config {
private const INTERNAL = "hidden";
}
echo Config::INTERNAL;
?>Fatal error: Uncaught Error: Cannot access private constant Config::INTERNAL in D:\xampp\htdocs\class-constant-visibility-violation.php:6
Stack trace:
#0 {main}
thrown in D:\xampp\htdocs\class-constant-visibility-violation.php on line 6
<?php
class Base {
final public const VERSION = "1.0";
}
class Sub extends Base {
public const VERSION = "2.0";
}
?>Fatal error: Sub::VERSION cannot override final constant Base::VERSION in D:\xampp\htdocs\final-class-constant-override.php on line 6
Stack trace:
#0 {main}