zoukankan      html  css  js  c++  java
  • php静态属性和静态方法

    php静态属性和静态方法

    本php教程主要是学习php中静态属性和静态方法的使用方法和基本的示例。

    ·                                 静态属性

    静态属性也就是说它的值保持其值,比如在类中实例化了N个对象,那么你可以在构造函数中定义一个静态属性来记住对象的个数。类中的静态属性和静态变量差不多,只不过在类中似乎又多了一个些使用上的限制罢了。让我们看看一般的变量吧:

    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. ?>  

    <?phpfunction test() { $n = 1; echo "The number is:$n<br />"; $n++;}test();test();test();test();test();test();?>

    很显然这个函数的结果如下:

    The number is:1

    The number is:1

    The number is:1

    The number is:1

    The number is:1

    The number is:1

    但是如果你的程序是这样:

    1. <?php   
    2. 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. ?>  

    <?phpfunction
    test() { static $n = 1; echo "The number is:$n<br
    />"; $n++;}test();test();test();test();test();test();?>

    我们只不过在变量名加了个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.在类中静态方法访问静态属性,使用类名::静态属性名即可调用类中的静态属性。 

     

    ·                                 静态方法

    1. <?php   
    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. ?>  
    1. <?php   
    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 "我现在余额为:";echo test :: getonemon();echo "<br>";
    13.     echo "消费后,我的余额为:";echo test :: gettwomon();
    14. ?>

    在这个示例里我们看到,使用了两种方法来访问静态属性$money的值:一种是前面都提到的类名::属性值的形式,另外一种则是使用了self关键字。当然推荐使用self关键字这种方式,因为如果那天不高兴了,我们修改的类名,那么如果你使用了第一种方式,你是不是还得修改下调用它的方法呢,当然你得在同一个类中,如果你是在子类中想调用父类的静态属性和方法,那就得使用parent::的方式了。

    再说一下

    1:如果你想在静态方法中调用其它静态方法时,请使用方法是:类名::方法名的形式进行调用,还是那句,如果你在同一个类进行这样的调用,就使用selft关键字进行调用吧。要不然你得的程序可就得报错了。

    2:php中静态方法不能调用非静态的属性和非静态方法,也不能使用类名::或者self::调用非静态属性。更不能使用$this->属性名来调用,总之在静态方法中只能调用静态属性或方法,非静态的无法调用。

  • 相关阅读:
    [纯奇技淫巧] 特征根
    杂题20200528
    杂题20200509
    杂题20200419
    杂题20200415
    杂题20200407
    杂题20200314
    Educational Codeforces Round 83 简要题解
    一种简单的dp trick
    杂题20200304
  • 原文地址:https://www.cnblogs.com/opret/p/4572217.html
Copyright © 2011-2022 走看看