php classes calling functions using :: and ->

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I can call a class using "->", but it complains about the ::
I see on the net where :: is used. Is there a good explanation on when
to use one over the other or the differences?

$help = new help();

class help{

    function foo(){
        echo "test";

Re: php classes calling functions using :: and ->

Quoted text here. Click to load it

:: is used to call class methods or properties when the class has not been  

Re: php classes calling functions using :: and ->

Anthony Smith kirjoitti:
Quoted text here. Click to load it

:: is used for calling a method by class, not object. This is the part  
where php becomes rocket science. You can call a method of a class  
without having created an instance of the class eq. an object by calling  
  it via the class.

So if you have

class help {
    function foo(){
        echo "Hello kitty!";

You can call the method foo without creating an object:

Or you can create an instance of the class:
$myhelp = new help();

and then use the class pointer to call the method

Why use :: instead of -> then? hmm... I dunno, it's kewl? I've only used  
it once with a singleton where it seemed to make sense buy it's actually  
quite rare I think. People who work with objects big-time may have use  
for it, but I don't think it's actually all that necessary. It's good to  
have and good to know what it is and what it does, but on most days  
you'll be fine without it.

"Olemme apinoiden planeetalla."

Re: php classes calling functions using :: and ->

Quoted text here. Click to load it

I think it is done to lower the overheads if for example you only need to  
use 1 function within the class. Instead of having the overhead of  
instantiating the class then calling the method (which I presume allocates  
memory for the methods and properties).  

Re: php classes calling functions using :: and ->

peter schreef:
Quoted text here. Click to load it
imho it would also be bad design / sort of misleading to create an  
object you only use for calling a function on that does not need any  
information from the object itself. With php5 you can also declare the  
function static, so that when the code is maintained (maybe years later  
by another programmer) it won't get modified to expect the object to be  
properly initialized.

To say it an other way, making a static function and using :: will  
communicate your intentions more clearly in your source code.

(i don't think the methods need extra memory for each extra object you  
instantiate. And php only adds properties that are either declared or  
actually assigned. But the object itself will probably be allocated, and  
deallocated when it is no longer referenced, so i think you are right  
about the overhead)


Henk Verhoeven,

Re: php classes calling functions using :: and ->

Quoted text here. Click to load it

It's really not all that rare -- in fact it's quite common.

As a matter of form, you should define class methods as static and
call them with ::  If you get into the habit of calling your instance
methods with :: you can run into problems because $this is not
available (and PHP will complain accordingly).  Furthermore there are
differences in inheritance between static methods and instance
methods  -- namely that there is no inheritance with static methods --
but that provides you with a certain performance benefit.

The short answer (and it's certainly not the right one in all cases)
is that if you use $this in your method, make it an instance method
and call it with ->.  If you don't use $this in your method, declare
it static and call it with ::

Re: php classes calling functions using :: and ->

Anthony Smith wrote:

Quoted text here. Click to load it

The difference between them is much the same as the difference between an
object and a class.

    class Person
        public $firstname;
        public $surname;
        public $spouse;

        public function __construct ($firstname, $surname)
            $this->firstname  = $firstname;
            $this->surname    = $surname;

            echo $this->get_name()." was born.\n";

        public function get_name ()
            return sprintf("%s %s",

        public function introduce_myself ()
            return "My name is ".$this->get_name()."\n";

        public static function wedding ($a, $b, $changename=FALSE)
            if (!empty($a->spouse) || !empty($b->spouse))
                return FALSE;

            $a->spouse = $b;
            $b->spouse = $a;
            printf("%s and %s are married!\n",

            if ($changename)
                $b->surname = $a->surname;

            return TRUE;

    $d = new Person('Dave', 'Jones');
    $m = new Person('Mary', 'Smith');
    Person::wedding($d, $m, TRUE);

The -> operator operates on objects, the :: operator operates on classes.

Toby A Inkster BSc (Hons) ARCS
Contact Me ~
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!

Site Timeline