:: Operator

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ngolehung84
Forum Newbie
Posts: 6
Joined: Tue Jan 30, 2007 8:45 am

:: Operator

Post by ngolehung84 »

Can you tell me about the :: operator in PHP?
thanks,
Hung.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
Post Reply