A static variable is accessed through class name followed by the scope resolution operator(::).
for Example:
( If class Name : A and Variable name is : x)
Then you can accessed it like this : A::x.
self keyword with scope resolution operator(::) is used to access “static variables” within class.
A Member function which is declared as “static” is accessed through class name with “scope resolution operator”.
Example 1:
<?php //definition of class. class User { public static $i=1; public $j=1; public function getValue() { return $this->j." value : " . self::$i; } public function setValue($a) { self::$i = $a; $this->j = $a; } } $User = new User(); $User->setValue(10); echo $User->getValue(); echo"<br>"; $User1 = new User(); echo $User1->getValue(); echo"<br>"; $User2 = new User(); echo $User2->getValue(); ?>
Example 2:
<?php // definition of class. class User { public $name; public $age; public static $minimumPasswordLength = 6; public function Describe() { return $this->name . " is " . $this->age . " years old"; } public static function ValidatePassword($password) { //if(strlen($password) >= self::$minimumPasswordLength) // return true; //else // return false; } public function set_name() { $this->name="Prakash"; } public function get_name() { return $this->name; echo"<br>"; } public function set_age() { $this->age=45; } public function get_age() { return $this->age; echo"<br>"; } } $password = "test"; if(User::ValidatePassword($password)) echo "Password is valid!"; else echo "Password is NOT valid!"; $User = new User(); echo $User->set_name(); echo"<br>"; echo $User->get_name(); echo $User->set_age(); echo"<br>"; echo $User->get_age(); ?>