I apologise if this seems obvious to the more experienced PHP users, however, I do believe I have missed an important block in my learning journey. When using objects, how do I now to call the function through -> or call it using :: . At the moment I have only been using the -> method however I have come across code using the :: method. Any insight or explanation is greatly appreciated!
Asked
Active
Viewed 1,118 times
2
-
2Its static scope. You can get more info at those links. [php manual oop5 static](http://php.net/manual/en/language.oop5.static.php) [php manual variable scope](http://php.net/manual/en/language.variables.scope.php) [php manual late static bindings](http://php.net/manual/en/language.oop5.late-static-bindings.php) Oh also class constans calling with the dots too. – Mehmet SÖĞÜNMEZ Jul 27 '16 at 07:15
-
Thanks, I'll be sure to read those links! – Oliver Chalk Jul 27 '16 at 07:17
2 Answers
5
You use ::
when you are calling a static method or variable, or when calling parent class (if current class is extended).
You use ->
when you are calling a method or variable that the class has to be instantiated.
Have a look Classes and Objects self and $this and Variable scope Static properties and variables.

miken32
- 42,008
- 16
- 111
- 154

matiaslauriti
- 7,065
- 4
- 31
- 43
1
http://php.net/manual/en/language.oop5.php
-> is for objects method and properties so when you create object of class you need to use ->
$someObject = new someClass();
$someObject->someMethod();
:: is for static method and properties of class, which means you do not need to create object to use it:
someClass::someMethod()

nospor
- 4,190
- 1
- 16
- 25
-
Thanks for clearing things up. So basically when you reference the object that is "static" you use the "::" However when you have the object instantiated in a variable you reference it using the ->. – Oliver Chalk Jul 27 '16 at 07:22
-
-
No, there is no such thing like static object. There is class. You can call directly class instead of create object from class. someClass::someMethod() - this is call static method *someMethod* of class *someClass*. You do not create objects to call static methods of class – nospor Jul 27 '16 at 07:29
-
Yeah, my bad. I said "Object" but yes the Class is the item that is static and is referenced using "::" – Oliver Chalk Jul 27 '16 at 07:32
-
@OliverChalk and wrong again. Class is not static. Method of class is static ;) – nospor Jul 27 '16 at 07:34
-