Home  >  Article  >  php教程  >  演示静态static

演示静态static

PHP中文网
PHP中文网Original
2016-05-26 08:18:461030browse

php代码:

<?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/>";
 
?>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn