What is the difference between & # 8594; and :: in PHP?

This thing has been looking for me for a long time, and I can’t find it anywhere!

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

Let me give you an example.

Imagine a class called MyClass and this class has a function myFunction

What is the difference between using:

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

or

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

thanks

+3
source share
4 answers

as indicated, "::" is for calling static methods, while "->" is, for example, method calls

unless parent :: is used to access functions in the 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();
+2
MyClass::myFunction();  // static method call

$myclass->myFunction(); // instance method call
+11

"::" . , :

MyClass::myStaticFunction()

:

MyClass->myStaticFunction()
+3
source
class MyClass {
  static function myStaticFunction(...){
  ...
  }

}

//$myObject=new MyClass(); it isn't necessary. It true??

MyClass::myStaticFunction();
0
source

Source: https://habr.com/ru/post/1724014/


All Articles