I’m pretty new to PHP and i’m trying to learn namespacing to a reasonable level.
I have found that I can use a function from another class by doing the following:
use Sonic\queries\projects;
$test = new projects();
$project = $test->getSingleProject($projectid);
However I can also access the function by simply doing the following:
$project = Sonic\queries\projects::getSingleProject($projectid);
Which is the best method to use and why? Or are they essentially the same? I’ve searched high and low for the answer but have had no success. Hopefully someone can explain this to me.
They are not the same. Using the
::notation calls the method statically, which means that the method cannot access any class instance variables (like$this->projectname). If the method does not make use of instance variables, then it is likely it should have been defined as a static method to begin with.PHP documentation on
staticmethods…