5

Possible Duplicate: What's the difference between :: (double colon) and -> (arrow) in PHP?
Reference - What does this symbol mean in PHP?

I'm quite decent with PHP, but only procedural. So I decided to venture forth into learning object oriented. I'm getting the hang of it, and liking it quite well.

On PHP.net, I've always seen object oriented as mysqli::query.

However, every example I've seen uses mysqli->query as well as what I have always used.

Today, I ran across actually seeing :: used in a class example.

So now, my question is, is there a difference between :: and ->? Is it like the difference between " and '?

4
  • 2
    You can use SymbolHound to find this stuff. Lots of answers here symbolhound.com/?q=%3A%3A+-%3E Commented Dec 16, 2012 at 1:10
  • Aaaand... Never mind. After an hour of searching, I found my answer AFTER I posted my question. XD. Commented Dec 16, 2012 at 1:11
  • And thanks elclanrs. I didnt see you comment before I made mine. =) Commented Dec 16, 2012 at 1:12
  • It is... Which is why I made a comment myself saying I found the answer afterwards. ;-) However, people were already answering, so I just left it be. Commented Dec 16, 2012 at 1:52

2 Answers 2

8

:: is for calling static methods, -> is for instance methods

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

5 Comments

In addition to this, methods are often referenced in documentation with ::, even when they are not static methods. I think that's where his confusion is.
Yeah, just like in Java you don't call methods using the pound sign (#), never understood why this is done.
That was EXACTLY why I asked Brad. Thanks Robin!
Since it answered your question, could you accept it by clicking the green tick please? ;)
In 1 min. (Literally. Haha)
4

:: is the "scope resolution operator" (also aptly named Paamayim Nekudotayim), and is usually used to do a static (which means that you'll call the method in the context of the class itself, not the object) method call. There are however exceptions to this rule, such as attempting to call a parent method from an overriden method:

parent::foo(); // uses same context as when the method itself was called

It'll also allow you to reference static properties of the class, such as static properties and constants.

ClassName::FOO; 
ClassName::$property = "bar";

-> is however used to reference a property or method in the actual object instance, and will always require an object instance to the left of the operator (such as $this).

1 Comment

+1 for linking me to the php.net page for it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.