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

所属分类: 网络编程 / PHP编程 阅读数: 2002
收藏 0 赞 0 分享

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(); 

更多精彩内容其他人还在看

Laravel实现批量更新多条数据

这篇文章主要介绍了Laravel实现批量更新多条数据,需要的朋友可以参考下
收藏 0 赞 0 分享

Laravel框架中队列和工作(Queues、Jobs)操作实例详解

这篇文章主要介绍了Laravel框架中队列和工作(Queues、Jobs)操作实例详解,需要的朋友可以参考下
收藏 0 赞 0 分享

Laravel框架数据库迁移操作实例详解

这篇文章主要介绍了Laravel框架数据库迁移操作实例详解,需要的朋友可以参考下
收藏 0 赞 0 分享

使用git迁移Laravel项目至新开发环境的步骤详解

这篇文章主要介绍了迁移Laravel项目至新开发环境的步骤详解,需要的朋友可以参考下
收藏 0 赞 0 分享

Laravel框架使用技巧之使用url()全局函数返回前一个页面的地址方法详解

这篇文章主要介绍了Laravel框架使用技巧之使用url()全局函数返回前一个页面的地址,需要的朋友可以参考下
收藏 0 赞 0 分享

Laravel5.3+框架定义API路径取消CSRF保护方法详解

这篇文章主要介绍了Laravel框架定义API路径取消CSRF保护,需要的朋友可以参考下
收藏 0 赞 0 分享

Laravel 5+ .env环境配置文件详解

这篇文章主要介绍了Laravel 5+ .env环境配置文件详解,需要的朋友可以参考下
收藏 0 赞 0 分享

Laravel5.5+ 使用API Resources快速输出自定义JSON方法详解

这篇文章主要介绍了Laravel5.5+ 使用API Resources快速输出自定义JSON方法详解,需要的朋友可以参考下
收藏 0 赞 0 分享

4种Windows系统下Laravel框架的开发环境安装及部署方法详解

这篇文章主要介绍了4种Windows系统下Laravel框架的开发环境安装及部署方法详解,需要的朋友可以参考下
收藏 0 赞 0 分享

解决Laravel5.x的php artisan migrate数据库迁移创建操作报错SQLSTATE[42000]

这篇文章主要介绍了解决Laravel5.x的php artisan migrate数据库迁移创建操作报错SQLSTATE[42000],需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多