php static静态属性和静态方法的调用
这里分析了php面向对象中static静态属性和静态方法的调用。关于它们的调用(能不能调用,怎么样调用),需要弄明白了他们在内存中存放位置,这样就非常容易理解了。静态属性、方法(包括静态与非静态)在内存中,只有一个位置(而非静态属性,有多少实例化对象,就有多少个属性)。
实例:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1633')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1633><?php
header("content-type:text/html;charset=utf-8");
class Human{
static public $name = "小妹";
public $height = 180;
static public function tell(){
echo self::$name;//静态方法调用静态属性,使用self关键词
//echo $this->height;//错。静态方法不能调用非静态属性
//因为 $this代表实例化对象,而这里是类,不知道 $this 代表哪个对象
}
public function say(){
echo self::$name . "我说话了";
//普通方法调用静态属性,同样使用self关键词
echo $this->height;
}
}
$p1 = new Human();
$p1->say();
$p1->tell();//对象可以访问静态方法
echo $p1::$name;//对象访问静态属性。不能这么访问$p1->name
//因为静态属性的内存位置不在对象里
Human::say();//错。say()方法有$this时出错;没有$this时能出结果
//但php5.4以上会提示
?>
静态属性
静态属性也就是说它的值保持其值,比如在类中实例化了N个对象,那么你可以在构造函数中定义一个静态属性来记住对象的个数。类中的静态属性和静态变量差不多,只不过在类中似乎又多了一个些使用上的限制罢了。让我们看看一般的变量吧:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy3644')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy3644>1.<?php
2.function test() {
3. $n = 1;
4. echo "The number is:$n<br />";
5. $n ;
6.}
7.test();
8.test();
9.test();
10.test();
11.test();
12.test();
13.?>
很显然这个函数的结果如下:
The number is:1
The number is:1
The number is:1
The number is:1
The number is:1
The number is:1
但是如果你的程序是这样:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2212')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2212>1.<?php2.function test() {
3. static $n = 1;
4. echo "The number is:$n<br />";
5. $n ;
6.}
7.test();
8.test();
9.test();
10.test();
11.test();
12.test();
13.?>
</td></tr></table>
我们只不过在变量名加了个static关键字而已,结果就大大的不同了:
The number is:1
The number is:2
The number is:3
The number is:4
The number is:5
The number is:6
1.static关键字可以用来修饰变量、方法(静态方法)
2.不经过实例化,就可以直接访问类中static的属性和static的方法。
3.static 的属性和方法,只能访问static的属性和方法,不能访问非静态的属性和方法。因为静态属性和方法被创建时,可能还没有任何这个类的实例可以被调用。
4.在当前类中如果要访问静态成员可以使用self::关键字进行访问。
5.在类中我们不能使用this关键来访问静态属性,因为静态属性在对象可能还没有实例化之前已经存在。
6.在类中静态方法访问静态属性,使用类名::静态属性名即可调用类中的静态属性。
静态方法
2.class test {
3. private static $money = 2000;
4. public static function getonemon() {
5. return test :: $money;
6. }
7. public static function gettwomon() {
8. self :: $money = self :: $money -1500;
9. return self :: $money;
10. }
11.}
12.echo "我现在余额为:";
13.echo test :: getonemon();
14.echo "<br>";
15.echo "消费后,我的余额为:";
16.echo test :: gettwomon();
17.?> </td></tr></table>
在这个示例里我们看到,使用了两种方法来访问静态属性$money的值:一种是前面都提到的类名::属性值的形式,另外一种则是使用了self关键字。当然推荐使用self关键字这种方式,因为如果那天不高兴了,我们修改的类名,那么如果你使用了第一种方式,你是不是还得修改下调用它的方法呢,当然你得在同一个类中,如果你是在子类中想调用父类的静态属性和方法,那就得使用parent::的方式了。
再说一下
1:如果你想在静态方法中调用其它静态方法时,请使用方法是:类名::方法名的形式进行调用,还是那句,如果你在同一个类进行这样的调用,就使用selft关键字进行调用吧。要不然你得的程序可就得报错了。
2:php中静态方法不能调用非静态的属性和非静态方法,也不能使用类名::或者self::调用非静态属性。更不能使用$this->属性名来调用,总之在静态方法中只能调用静态属性或方法,非静态的无法调用。
结论:
(1)、静态属性不需要实例化即可调用。因为静态属性存放的位置是在类里,调用方法为"类名::属性名";
(2)、静态方法不需要实例化即可调用。同上
(3)、静态方法不能调用非静态属性。因为非静态属性需要实例化后,存放在对象里;
(4)、静态方法可以调用非静态方法,使用 self 关键词。php里,一个方法被self:: 后,它就自动转变为静态方法;(会提示strict)
您可能感兴趣的文章:
php中对static关键字的理解
php中static关键字的理解
PHP 中的 -> 和 :: 的区别
php static静态属性和静态方法的调用
深入理解c# static 静态数据成员和方法
C# 静态方法与非静态方法的区别分析
PHP面向对象中new self( )和 new static( ) 的区别
PHP中static和self的区别
php静态变量的作用是什么?
PHP面向对象之旅:static变量与方法