1

Given a piece of code such as:

{$globalScript->qtip(active_page::getCurrentPageName()=='factsheet','../')}

What's the difference in usage in PHP 5 between "->" and "::"?

3 Answers 3

5

-> calls the instance (object) method and :: calls the class method, which is defined with the keyword static.

You can read more about that here : http://php.net/manual/en/language.oop5.php and especially here : http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php.

Sign up to request clarification or add additional context in comments.

2 Comments

you need the word static in there
Thanks, I added something about it.
2

in -> you are calling a public method in an object instance

and in :: you are calling a static method

Ex:

Class MyClass {
    public function doStuff(){
        // stuff
    }

    public static function doStaticStuff (){
       // other stuff
    }
}

$obj = new MyClass();
$obj->doStuff(); // works


// in static you dont need to creat a new object
MyClass::doStaticStuff();

MyClass::doStuff(); // will fail here

// but you can also call the static method on an existing object
$obj::doStaticStuff();

Comments

1

-> is used in object instance, where :: is used in class methods.

Basically, the :: is used for static methods and properties.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.