Oliver Chalk Oliver Chalk - 4 months ago 5
PHP Question

In PHP when do I use :: and when do I use ->

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!

Answer

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