-2

Possible Duplicates:
Reference - What does this symbol mean in PHP?
In PHP, whats the difference between :: and -> ?

When you try to access property of method inside class whats the difference between :: and -> and is there full reference of operators related to object oriented programming in php somewhere?

Community
  • 1
  • 1
JohnBixXE
  • 7
  • 1
  • 2

3 Answers3

7

The :: is for static properties and methods, e.g.

 MyClass::create();

The -> is for when you have an object instantiated from the class, e.g.

$myObject = new MyClass;
$myObject->create();
drewm
  • 2,003
  • 1
  • 16
  • 22
2

When using :: you can access a Class method statically without creating an instance of the class, something like:

Class::staticMethod();

You would use -> on an instance of a Class, something like:

$class = new Class();
$class->classMethod();
Tomgrohl
  • 1,767
  • 10
  • 16
0

It's the difference between static and dynamic properties and methods.

Observe this piece of code to appreciate the difference:

class MyClass {

    protected $myvar = 0;
    public static  $othervar = "test";
    public function start($value)
    {
         // because this is not static, we need an instance.
         // because we have an instance, we may access $this
         $this->myvar = $value;

         // although we may still access our static variable:
         echo self::$othervar;
    }


    static public function myOtherFunction ($myvar)
    {
        // its a static function, so we're not able to access dynamic properties and methods ($this is undefined)

        // but we may access static properties
        self::$overvar = $myvar;
    }
}

Literature for your conveinience:

Community
  • 1
  • 1
Arend
  • 3,741
  • 2
  • 27
  • 37