0

Possible Duplicate:
In PHP, whats the difference between :: (double colon) and -> (arrow)?

I just had a quick question about what seems to me to be two different ways of doing the same thing. If there are difference other than syntax, please explain what they are.

We'll assume we have a class called MyClass with a method called myMethod. What's in them probably isn't relevant, because we're just going to be calling them from another file.

Here's the first way that I know of (there may be other ways - these are what I know):

$myvar = new MyClass();
$myvar->myMethod();

And the second way:

MyClass::myMethod();

If there are other ways, particularly better ways, by all means elaborate on them, but the main question here is the difference between these two examples.

Thanks!

Community
  • 1
  • 1
Clowerweb
  • 1,766
  • 2
  • 14
  • 13
  • 1
    *(related)* http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php – Gordon Dec 30 '11 at 22:56
  • 2
    I suspect you're going to get a lot of noise and not a real answer to your question. When you run a method on an object, you're running it in relation to that object's properties and object-referencing methods. When you do a static call, you're not; you're running it against a global class that has only one "instance", **itself**. You also have `self::$prop`, `self::method()`, `$this->prop`, `$this->method()`, etc. (if I've missed one, like `parent`). It has to do with context, the first relying on the current instance object's *context*, the second on a *global class* context. – Jared Farrish Dec 30 '11 at 23:00
  • 1
    @Gordon You're right that it's a duplicate of your first comment, I apologize - I tried searching and didn't see that question. Thanks for the reference :D – Clowerweb Dec 30 '11 at 23:12

3 Answers3

4
$myObj = new MyClass();
$myObj->myMethod();

This is an instance method call. You are calling it with an object reference (passed as $this inside the method definition) that you just created in the previous line.

MyClass::myMethod();

This is a static method call. You are calling it as is, with no reference. Only static methods can should be called this way.


As pointed out in the comments, instance methods can be called with the :: syntax, but using $this in any way will generate a runtime error. This is also something you wouldn't normally do. I can't think of an scenario where this is in any way useful. So stick to the above.

Darkhogg
  • 13,707
  • 4
  • 22
  • 24
3

The first one executes a method on an object. The second one executes a method at class level (a classmethod).

They are both different things.

ChristopherS
  • 853
  • 4
  • 16
  • Perhaps it's noteworthy to add that static methods usually (but not always) take the form of 'utility' funtions. For instance (excuse the pun) a static function like `ReverseString` (in it's simplest form) only needs the string that you pass to it, it doesn't need the instance of an object to do that, so you wouldn't have to create a new object first on which you call `ReverseString`, rather you would call it directly. – diggingforfire Dec 30 '11 at 22:56
2

Use MyClass::myMethod(); with static methods. And $myvar->myMethod(); in other cases.

http://php.net/manual/en/language.oop5.php

Wouter Dorgelo
  • 11,770
  • 11
  • 62
  • 80