This article explains the static attributes and static methods in PHP with examples. Share it with everyone for your reference. The details are as follows:
<?php /* * static */ /*静态:属于类而不属于单个对象 (全局的,所有对象共享的) *静态属性:类的方法内调用静态属性时,不要使用$this->方式,而要使用self::的方式 *静态方法: *在类没有任何对象的时候也能被调用 *当成普通方法来用也没问题的 *在静态方法中不能调用普通方法 * * */ class xin { static private $name; public function setname($namec) { self::$name = $namec; } public function getname() { return self::$name; } static public function name($namecc) { echo "I am $namecc"; } } $xind = new xin(); $xind->setname("地方 <br/>"); echo $xind->getname(); $oldd = new xin(); $oldd->setname("政府 <br/>"); echo $oldd->getname(); echo $xind->getname(); echo xin::name("星星"); echo "<br/>"; ?>
The running results are as follows:
Place
Government
Government
I am stars
I hope this article will be helpful to everyone’s PHP programming design.