PHP OOP static members

Static variable and methods are called class variable and methods, they are not part of an object and unable to access variable and methods using object.
To access static variable and methods use scope resolution operator(::).
class Person{
	public static $name="Aman";
	public static function display(){
		echo "Name:". self::$name ."<hr>";
	}
}
$person= new Person();
echo Person::$name;
Person::display();

Static properties with inheritance

class Account{
	public static $name="Aman";	
}

class User extends Account{
	public static $name="Rama";
	public function show(){
		echo parent::$name;
		echo self::$name;
	}
}

$user = new User();
$user->show();
© 2016 - 2022, All Rights are Reserved.