dongwei1855 2009-11-26 15:03
浏览 33
已采纳

PHP中的 - >和::有什么区别?

This thing has been bugging me for long and I can't find it anywhere!

What is the difference when using classes in php between :: and ->

Let me give an example.

Imagine a class named MyClass and in this class there is a function myFunction

What is the difference between using:

MyClass myclass = new MyClass
myclass::myFunction();

or

MyClass myclass = new MyClass
myclass->myFunction();

Thank you

  • 写回答

4条回答 默认 最新

  • dsilhx5830 2009-11-26 15:32
    关注

    as stated, "::" is for static method calls whereas "->" is for instance method calls

    except for when using parent:: to access functions in a base class, where "parent::" can be used for both static and non-static parent methods

    abstract class myParentClass
    {
       public function foo()
       {
          echo "parent class";
       }
    }
    
    class myChildClass extends myParentClass
    {
       public function bar()
       {
          echo "child class";
          parent::foo();
       }
    }
    
    $obj = new myChildClass();
    $obj->bar();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?