Link to home
Start Free TrialLog in
Avatar of andieje
andieje

asked on

when to use :: in php

hey all (ray and dave?)

I i was my understanding in oo php that -> was foor instance properties and methods and :: was for startics

This page from the manual says (i do read the manual)

The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.

I'll probably never do that so not to concerned, But what does the comment with 46 ticks mean on here
http://stackoverflow.com/questions/3173501/whats-the-difference-between-double-colon-and-arrow-in-php

as its related but beyond me. I learnt progrmming in the days when you wrote a config file and then type program.render ;

Seriously i did no null terminated strings, pointers, stacks, lists, distributed objectscorbra etc etc but it was 20 years ago and i had a human being to explain not badly written wrb page (except yours of course). And those nice strongly typed languages make sense to me (please not debate - just personal perfernce; i like the colour purple and badminton but im not saying pink and tennis are crap)

Thanks
Avatar of Gary
Gary
Flag of Ireland image

Use -> to refer to a member of the object.

Use :: to refer to a static member of the class.

So you can use :: to call the static function without referencing the object
To call a member of the class you need to reference the object ->

<?php
error_reporting(E_ALL);

class myClass {
    public static function function1() {
        echo "Static function<br>";
    }

    public function function2() {
        echo "Member of the class<br>";
    }
}

$myClass = new myClass;
echo $myClass->function1();
echo $myClass->function2();

echo myClass::function1();

//This is wrong but will still work and just throw a warning.
echo myClass::function2();

Open in new window

Let's say I create a Class definition that is designed to be used to make an instance of an object.  I would instantiate the object (make an instance from the class) with the keyword new.  Now I have a data structure in my PHP script and I can do various things with it, such as set and retrieve properties and call methods.  The object itself contains data that gets used by the methods of the object.  Because it contains data that can change, this object is said to be "dynamic."  When I refer to the object (not the class) I use the -> notation to identify the object and the methods or properties of the object, like this:

$obj->method();
$obj->property;

Open in new window


But what if I do not need any changeable data in the object?  It's possible that I don't need any instance of the object at all.  I just need to be able to use some of the programming that is inside a class definition, and I don't want to recreate the programming all over again. In this case I can call the methods of the class statically.  I do not use the keyword new so there is no object created, and the methods of the class cannot use the $this-> keyword (because there is no object instance, so there is nothing for $this-> to refer to).  In this case, I can use the double-colon, called the scope resolution operator, to name the class and method.
class::method();

Open in new window


Here's a brief example.  I'll create two classes - one is a Cat, and it is used to create objects.  The other is Math, and it's used to perform computations, but does not create an object instance.  Note the different notation using -> and :: in the different ways we call these code structures.
<?php // demo/temp_andieje.php
error_reporting(E_ALL);


// SEE: http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/Q_28571126.html


// A CLASS THAT WILL HAVE AN OBJECT INSTANCE
Class Cat
{
    public $noise = 'Meow';
    public function setVoice($x)
    {
        $this->noise = $x;
    }
    public function speak()
    {
        echo PHP_EOL . $this->noise;
    }
}

// INSTANTIATE A CAT
$kitty = new Cat;
$kitty->speak();

// INSTANTIATE A DIFFERENT CAT
$tiger = new Cat;
$tiger->setVoice('Roar');
$tiger->speak();

// SHOW WHAT A CAT OBJECT LOOKS LIKE (HAS PROPERTIES)
echo PHP_EOL;
var_dump($tiger);


// A WORKER CLASS THAT DOES NOT NEED TO CREATE ANY OBJECT BUT IS STILL USEFUL
Class Math
{
    public static function add($a, $b)
    {
        return $a + $b;
    }
    public static function multiply($a, $b)
    {
        return $a * $b;
    }
}

// USE THE MATH CLASS TO DO MATH, USING A STATIC CALL TO A METHOD OF THE CLASS
$x = 3;
$y = 2;
echo PHP_EOL . Math::add($x, $y);

Open in new window

HTH, ~Ray
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of andieje
andieje

ASKER

i do get the static vs instance. It was the comment on stackoverflow in the question that  threw me
The answer there is a bit confusing
Avatar of andieje

ASKER

super