欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

php类怎么给静态变量赋值

 

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();# 0
hello();# 1
hello();# 2
 

523826219 

采纳率:66% 擅长: C/C++ 数据库DB PHP JavaScript Html/Css

其他回答

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');
?>
 本回答被提问者和网友采纳
来自 https://zhidao.baidu.com/question/425219942573678652.html
普通分类: