->和有什么区别?和::在PHP中? [英] What is the difference between -> and :: in PHP?

查看:321
本文介绍了->和有什么区别?和::在PHP中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个东西困扰了我很久了,我在任何地方都找不到!

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

在PHP中使用::和->

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

让我举个例子.

想象一下一个名为MyClass的类,该类中有一个函数myFunction

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

使用之间有什么区别

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

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

谢谢

推荐答案

如上所述,"::"用于静态方法调用,而->"用于实例方法调用

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

除了使用parent ::访问基类中的函数外,其中"parent ::"可用于静态和非静态父方法

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

这篇关于->和有什么区别?和::在PHP中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆