PHP中如何形成static::与new static()的静态绑定
发布:smiling 来源: PHP粉丝网 添加日期:2022-06-19 09:29:54 浏览: 评论:0
PHP中static::与new static()之后期静态绑定
一、parent、self、$this、__CLASS__
- class A {}
- class B extends A{
- parent::(public|protected)(静态方法|静态变量) ===> parent永远是A
- self::(public|protected)(静态方法|静态变量) ===> self永远是B
- $this->(public|protected)(非静态方法|非静态变量) ===> $this永远是B的是实例化对象
- __CLASS__ ===> 永远是B
- }
- class C extends B{
- parent::(public|protected)(静态方法|静态变量) ===> parent永远是B
- self::(public|protected)(静态方法|静态变量) ===> self永远是C
- $this->(public|protected)(非静态方法|非静态变量) ===> $this永远是C的是实例化对象
- __CLASS__ ===> 永远是C
- }
二、static::
static关键字可以实现以下功能:
1 调用类的静态方法 有后期静态绑定效果;
2 调用类的静态属性 有后期静态绑定效果;
3 调用类的非静态方法 没有后期静态绑定效果;
4 注意:不可以调用非静态属性;
- class A {
- private static function foo() {
- echo "A success!\n";
- }
- public function test() {
- $this->foo();
- }
- }
- class B extends A {
- }
- class C extends A {
- private static function foo() {
- echo "C success!\n";
- }
- }
- $b = new B();
- $b->test();//A success!
- $c = new C();
- $c->test();//A success!
- class A {
- private static function foo() {
- echo "A success!\n";
- }
- public function test() {
- static::foo();
- }
- }
- class B extends A {
- }
- class C extends A {
- private static function foo() {
- echo "C success!\n";
- }
- }
- $b = new B();
- $b->test();//A success!
- $c = new C();
- $c->test();//A无法调用C里的私有foo方法
- //将C的foo改成非private(public|protected)就可以解决
- class A {
- private static function foo() {
- echo "A success!\n";
- }
- public function test() {
- static::foo();
- }
- }
- class B extends A {
- }
- class C extends A {
- public static function foo() {
- echo "C success!\n";
- }
- }
- $b = new B();
- $b->test();//A success!
- $c = new C();
- $c->test();//C success!
- class A {
- public static function foo() {
- static::who();
- }
- public static function who() {
- echo __CLASS__."\n";
- }
- }
- class B extends A {
- public static function test() {
- A::foo();
- parent::foo();
- self::foo();
- }
- public static function who() {
- echo __CLASS__."\n";
- }
- }
- class C extends B {
- public static function who() {
- echo __CLASS__."\n";
- }
- }
- C::test();
A =>A::foo()的结果
C =>parent::foo()能走到A的foo,里面static::who找C::who
C =>self::foo()能走到B的foo,B继承A,走到A的foo,里面static::who找C::who
- class A {
- protected static function foo() {
- static::who();
- }
- protected static function who() {
- echo __CLASS__."\n";
- }
- }
- class B extends A {
- public static function test() {
- A::foo();
- parent::foo();
- self::foo();
- }
- protected static function who() {
- echo __CLASS__."\n";
- }
- }
- class C extends B {
- protected static function who() {
- echo __CLASS__."\n";
- }
- }
- C::test(); //A C C,解释同上
- class A {
- public static function foo() {
- static::who();
- }
- private static function who() {
- echo __CLASS__."\n";
- }
- }
- class B extends A {
- public static function test() {
- A::foo();
- parent::foo();
- // self::foo();
- }
- private static function who() {
- echo __CLASS__."\n";
- }
- }
- class C extends B {
- private static function who() {
- echo __CLASS__."\n";
- }
- }
- C::test();
- //A =>A::foo()的结果
- //报错 A不可C的私有方法who => parent::foo()能走到A的foo,里面static::who找C::who,C的who只能在C里调用,不能在A里调用
- //报错 A不可C的私有方法who => self::foo()能走到B的foo,B继承A,走到A的foo,里面static::who找C::who,C的who只能在C里调用,不能在A里调用
三、new static()
- //new self()与new static()的区别,官网例子如下:
- class A {
- public static function get_self() {
- return new self();
- }
- public static function get_static() {
- return new static();
- }
- }
- class B extends A {}
- echo get_class(B::get_self()); // A
- echo get_class(B::get_static()); // B
- echo get_class(A::get_static()); // A
Tags: static:: new static()
- 上一篇:PHP中$是什么意思?
- 下一篇:最后一页
相关文章
- ·PHP中new static() 和 new self() 的区别介绍(2021-05-08)
- ·PHP的new static和new self的区别与使用(2022-01-24)
推荐文章
热门文章
最新评论文章
- 写给考虑创业的年轻程序员(10)
- PHP新手上路(一)(7)
- 惹恼程序员的十件事(5)
- PHP邮件发送例子,已测试成功(5)
- 致初学者:PHP比ASP优秀的七个理由(4)
- PHP会被淘汰吗?(4)
- PHP新手上路(四)(4)
- 如何去学习PHP?(2)
- 简单入门级php分页代码(2)
- php中邮箱email 电话等格式的验证(2)