php基础知识:类与对象(5) static

时间:2023-07-24 03:01:30 

Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can). 
声明静态的类变量和方法可以不需要实例化类对象的情况下对他们进行调用。静态类不能被类对象调用。(类的静态方法可以)。//注意看第一个例子,在一个非静态的方法中调用了静态的变量。唯一的不同是用了self。难道用了self就可以????不知道???需要一个试验。

The static declaration must be after the visibility declaration. For compatibility with PHP4, if no visibility declaration is used, then the member or method will be treated as if it was declared as public. 
静态声明必须必须是显式的声明。为了兼容PHP4,如果没有显式声明的对象或者方法,被当作声明为public。

Because static methods are callable without an instance of the object created, the pseudo variable $this is not available inside the method declared as static. 
因为静态方法不需要实例化类对象来调用,所以伪变量$this在静态方法中也是不可用的。 

In fact static method calls are resolved at compile time. When using an explicit class name the method is already identified completely and no inheritance rules apply. If the call is done by self then self is translated to the current class, that is the class the code belongs to. Here also no inheritance rules apply. 
实际上,静态的方法调用在编译时已经确定了。(这段我不会翻译。???不明白???)
求了很久求来的翻译如下:
------------------------------------------------
实际上,静态方法的调用在编译时解决。当使用一个明确的类名时,方法已经被完全识别而不需要应用继承规则。如果由自身调用,那么自身被解析成当前的类,也就是代码所属的类。这里也没有应用继承规则。
但是一个新的问题:
这里不一定有继承产生,为什么会提到继承规则?(???不明白????)

Static properties cannot be accessed through the object using the arrow operator ->. Calling non-static methods statically generates an E_STRICT level warning. 
静态成员不能被类的对象通过箭头符号->来调用。静态的调用一个非静态方法会导致一个E_STRICT级别的警告。

静态成员例:

class Foo 

   public static $my_static = 'foo';  
   public function staticValue() { 
       return self::$my_static;//注意这里!!!! 
       //return $my_static;//这样写会不会出错。需要试验 
   } 


class Bar extends Foo 

   public function fooStatic() { 
       return parent::$my_static;//注意这里!!!! 
   } 

print Foo::$my_static . " n"; 
$foo = new Foo(); 
print $foo->staticValue() . " n"; 
print $foo->my_static . " n";      // 未定义的"Property" my_static  
// $foo::my_static is not possible 
print Bar::$my_static . " n"; 
$bar = new Bar(); 
print $bar->fooStatic() . " n"; 

静态方法例: 
class Foo { 
   public static function aStaticMethod() { 
       // ... 
   } 

Foo::aStaticMethod(); 

标签:php基础知识:类与对象(5),static
0
投稿

猜你喜欢

  • 利用Python实现智能合约的示例详解

    2022-04-06 00:45:23
  • sqlserver主键设计的注意点

    2012-08-21 10:42:44
  • python 绘图模块matplotlib的使用简介

    2023-08-09 19:09:01
  • IE中雅黑字体给布局带来的变化

    2008-06-13 11:22:00
  • Python操作dict时避免出现KeyError的几种解决方法

    2022-12-30 14:48:26
  • 在MySQL中使用Sphinx实现多线程搜索的方法

    2024-01-22 17:25:45
  • sqlserver数据库主键的生成方式小结(sqlserver,mysql)

    2012-08-21 10:25:45
  • Python简单读写Xls格式文档的方法示例

    2021-11-02 13:27:30
  • Python操作MongoDB数据库的方法示例

    2022-09-03 13:48:40
  • mysql使用LOAD语句批量录入数据方法

    2010-03-09 16:31:00
  • Python入门Anaconda和Pycharm的安装和配置详解

    2022-12-21 20:25:07
  • python3实现Dijkstra算法最短路径的实现

    2023-10-14 13:17:40
  • Python3 类型标注支持操作

    2021-12-25 05:39:41
  • python3.0 模拟用户登录,三次错误锁定的实例

    2022-07-23 01:35:48
  • 浅谈解决360兼容模式浏览器的方法

    2023-09-17 01:11:39
  • 如何在golang中使用shopspring/decimal来处理精度问题

    2024-05-21 10:24:12
  • python将多个文本文件合并为一个文本的代码(便于搜索)

    2021-10-23 07:21:27
  • 向上不间断(无缝)滚动图片js代码

    2007-09-24 20:22:00
  • python使用tornado实现简单爬虫

    2022-07-07 12:03:08
  • sql添加数据后返回受影响行数据

    2011-11-03 17:18:18
  • asp之家 网络编程 m.aspxhome.com