转:http://zhidao.baidu.com/question/319449701.html
<?php
$num=0;
function show(){
global $num;
$num++;
echo "第".$num."次调用";
}
show();
show();
show();
//静态变量跟踪同一个函数
function show(){
static $num;
$num++;
echo "第".$num."次调用";
}
show();
show();
show();
class Model{ //定义类Model
private $mysqli; //私有变量
static $dbON;//数据库连接状态,静态变量
function __construct(){ //定义构造函数,每当 执行new() 时调用
$this->connect(); //调用方法 connect--连接数据库
}
private function connect(){ 定义方法 connect--功能是 连接数据库
if(is_null(self::$dbON)){
include 'db_config.php';
echo "<h1>111111111111</h1>";
}
self::$dbON=TRUE;
$this->mysqli=HOST."==".USER."==".PWD."==".DBNAME;
echo $this->mysqli."<br/>";
}
}
$channel = new Model(); 新建对象channel,建立了到数据库的连接
$arc = new Model(); //虽然又新建了一个对象,但是因为有了static $dbON(=非空),所以没有新建数据连接,只是引用了channel的数据库连接,以下类似。
$arc = new Model();
$arc = new Model();
$arc = new Model();
$arc = new Model();
$arc = new Model();
$arc = new Model();
$arc = new Model();
$arc = new Model();
?>
<?php
function do_something(){
/*$first_time=false;*/
static $first_time=false;//与第二种的区别
if(!$first_time){
echo "first";
$first_time=true;
}
echo "third";
}
do_something();
echo "<br>";
do_something();
?>
1 <?php
2 class student{
3 public $name;
4 public static $score;
5 public function __construct($name=''){
6 $this->name=$name;
7 }
8 public function getname(){
9 return $this->name;
10 }
11
12 public function getscore(){
13 return self::$score;
14 }
15
16 public function setscore($score){
17 self::$score=$score;
18 }
19 }
20 student::$score=60;
21 $lilei=new student('lilei');
22 $hanmeimei=new student('hanmeimei');
23 $lilei::$score=89;
24 $lilei->name='ll';
25
26 $hanmeimei::$score=90;
27 $hanmeimei->name='hmm';
28 echo $lilei->getscore();
29 echo "<br>";
30 echo $lilei->name;
31 echo "<br>";
32
33 $hanmeimei->setscore(100);
34 $lilei->setscore(99);
35 echo $hanmeimei->getscore();