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();
猜你喜欢
- 以下函数采用FSO对象,文件位置在FSO.ASP。FSO对象的文件编码属性只有三种,系统默认,Unicode,ASCII,并没有我们要的ut
- 环境Laravel 5.4原理在Laravel中,门面为应用服务容器中绑定的类提供了一个“静态”接口
- sysdate+(5/24/60/60) 在系统时间基础上延迟5秒 sysdate+5/24/60 在系统时间基础上延迟5分钟 sysdat
- 原文地址:30 Days of Mootools 1.2 Tutorials - Day 4 - Functions函数和MooTools
- ASCII(str) 返回字符串str的第一个字符的ASCII值(str是空串时返回0)mysql> select ASCII(
- MySQL的ODBC接口实现是通过安装MyODBC驱动,这个驱动程序是跨平台的。如果在Linux等Unix体系操作系统下使用,需要先安装Io
- 代码如下: 代码如下:<% '用ASP获取远程目标网页指定内容On Error Resume Next Server.Scri
- 下面是一份在 HTML 4 Strict 和 XHTML 1.0 Strict 下必须遵守的标签嵌套规则,比如你不能在 <a>
- Session 对象 可以使用 Session 对象存储特定用户会话所需的信息。这样,当用户在应用程序的 Web 页之间跳转时,存储在 Se
- 使用timer来统计asp页面程序的运行时间。实例代码和说明见下:<%Dim varInitial_TimevarIniti
- Microsoft建立了一种既灵活又强大的安全管理机制,它能够对用户访问SQL Server服务器系统和数据库的安全进行全面地管理。按照本文
- 大家都在关注视觉的盛宴,西方的美学;今天就分享下,中国最为古老的美,也是身边随处可见的美学–中国汉字书法之美;古文者,仓颉做造也。仰观奎星园
- 如果你能很好的理解我下面的一句话,那这些都不是问题了。asp是服务器端语言,它的作用是动态生成客户端浏览器所能识别的html css jav
- 导读:由于banner一般用于专题类网站,在门户网站的二级页面,用户进来之前,在首页已经对主题有一定的了解和认识,所以banner的作用是在
- Python CET自动查询方法需要用到的python方法模块有:sys、urllib2本文实例讲述了Python实现CET查分的方法。分享
- 在按钮旁边加文字1.打开editor/js/ 两个js文件fckeditorcode_gecko.js fckeditorcode_ie.j
- 当然这应该属于正常过滤手法,而还有一种过滤HTML标签的最终极手法,则是将一对尖括号及尖括号中的所有字符均替换不显示,该方法对于内容中必须描
- 1969年8月8日,在北京协和医院降生了一个漂亮的小女孩。接生的阿姨说,她的声音这么大,好象想要全世界的人都听到。后来,她的父亲为她取了一个
- 代码如下:'============================== '格式化HTML,SDCMS加强版 '==
- 我看见朋友可以把数据库的记录输出到页面表格上去,觉得很有用。这是怎么做的啊?见下:dbtable.asp<html><he