Page 1 of 1

:: Operator

Posted: Wed Feb 14, 2007 3:25 am
by ngolehung84
Can you tell me about the :: operator in PHP?
thanks,
Hung.

Posted: Wed Feb 14, 2007 3:40 am
by Chris Corbyn
It calls a function in your class statically (i.e. outside of Object context so $this doesn't exist) which basically makes it stateless. In PHP5 you can access static class variables with it too.

PHP4

Code: Select all

class Foo
{
    function doSomething() {}
}

echo Foo::doSomething();
PHP5

Code: Select all

class Foo
{
    public static function doSomething() {}
}

echo Foo::doSomething();
http://uk.php.net/manual/en/language.oo ... otayim.php

Posted: Wed Feb 14, 2007 5:19 am
by Jenk
It also has a pseudo use for calling parent object methods/properties where you have overloaded with a child class, a good example is the constructor. (php5 example below)

Code: Select all

class Parent
{
    public function __construct()
    {
        $this->doSomething();
    }
}

class Child extends Parent
{
    public function __construct()
    {
        parent::__construct();
    }
}

$foo = new Child; // doSomething() is executed.