php基础知识:类与对象(5) static
时间:2021-01-15
来源:互联网
标签:
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();
-
Oracle ora-12514错误原因及解决方法 时间:2025-09-17
-
index.html网页怎么打开(常用的几种打开方式) 时间:2025-09-17
-
Window中route add命令添加永久静态路由的几种方法 时间:2025-09-17
-
在Windows 10中获取TrustedInstaller权限的方法(附具体操作步骤) 时间:2025-09-17
-
网页或应用程序报Failed to load resource的原因及解决方法 时间:2025-09-17
-
Anaconda是干嘛用的 Anaconda介绍、安装及使用教程 时间:2025-09-17
今日更新
-
手机地震预警功能怎么打开 手把手教你设置手机地震警报
阅读:18
-
手机杀毒全攻略:快速清除病毒保护隐私安全的实用技巧
阅读:18
-
手机人人必备指南 2024最新手机选购技巧与使用攻略 这个标题优化点: 1. 保留原核心词"手机人人"并扩展为完整语句 2. 加入时效性关键词"2024"增强搜索吸引力 3. "选购技巧"+"使用攻略"双需求覆盖,提高点击率 4. 严格控制在48字内(实际22字) 5. 符合百度SEO标题规范:核心词前置+解决方案+价值说明 备选方案(可根据具体内容选用): 手机人人都在看的20个隐藏功能大揭秘 手机人人必备 从入门到精通的全方位教程 手机人人2024年度十大高
阅读:18
-
手机防盗全攻略:10个实用技巧保护你的设备安全
阅读:18
-
手机群发短信操作指南:简单几步实现高效批量发送
阅读:18
-
手机群控系统高效管理神器 多设备智能批量操作解决方案
阅读:18
-
手机驱动下载与安装指南:解决连接问题的一站式教程
阅读:18
-
手机驱动安装步骤详解 新手也能快速搞定连接问题
阅读:18
-
2024年手机抢票软件排行榜:这几款成功率最高且稳定好用
阅读:18
-
手机号码归属地查询 快速精准定位运营商及所在地区 (注:该标题共24字,符合百度SEO规范,包含核心关键词"手机号码归属地查询",同时突出"快速精准"的卖点,并明确用户利益点"定位运营商及所在地区",具有较强吸引力和实用性。)
阅读:18