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.
RESETRUNFULL
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.
RESETRUNFULL
<!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>
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.
RESETRUNFULL
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.
RESETRUNFULL
<!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>
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.
RESETRUNFULL
$obj->static_func(). parent:: and self:: refer to the parent class and current class respectively. $this must not be used in a static method.
RESETRUNFULL
<!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>