欢迎各位兄弟 发布技术文章
这里的技术是共享的
case-1:给类中的静态变量赋值:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | class PHPJungle{ private static $__instance = null;# 类中的私有成员:<a href="https://www.baidu.com/s?wd=%E9%9D%99%E6%80%81%E5%8F%98%E9%87%8F&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dBnhnvPhDduHb1PjcvmHfd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPjcdnWD4rHfzPHT1PWT3PWRz" target="_blank" class="baidu-highlight">静态变量</a> public function __construct(){ } public static function getInstance(){ if(self::$__instance instanceof self) return self::$__instance; # 给<a href="https://www.baidu.com/s?wd=%E9%9D%99%E6%80%81%E5%8F%98%E9%87%8F&tn=44039180_cpr&fenlei=mv6quAkxTZn0IZRqIHckPjm4nH00T1dBnhnvPhDduHb1PjcvmHfd0ZwV5Hcvrjm3rH6sPfKWUMw85HfYnjn4nH6sgvPsT6KdThsqpZwYTjCEQLGCpyw9Uz4Bmy-bIi4WUvYETgN-TLwGUv3EPjcdnWD4rHfzPHT1PWT3PWRz" target="_blank" class="baidu-highlight">静态变量</a>赋值 return new self(); } public function anyMethod(){ echo 'Hello world!','<hr>'; return; }}$PJ = new PHPJungle();$PJ->getInstance()->anyMethod(); |
case-2:给函数中的局部静态变量赋值:
1 2 3 4 5 6 7 8 9 | function hello(){ static $total = 0; echo $total,'<hr>'; $total++; # 给函数中的局部静态变量赋值}hello();# 0hello();# 1hello();# 2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php class info{ public static $height; public static $age; function __construct($height,$age){ self::$height = $height; self::$age = $age; self::display(); } public static function display(){ echo self::$height.' '.self::$age; } } $info = new info('178','23');?> |