Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

7
  • $bar is a local variable, in setBar()'s scope, since Foo doesn't have a member called $bar. Commented Jul 30, 2010 at 23:13
  • 7
    @NullUserException: But now you just removed a feature from PHP: being able to dynamically add members to an object. Commented Jul 30, 2010 at 23:14
  • 3
    @NullUserException: Because the two actions are semantically different. When you use $this, you are referring to an instance of a class. When you are using self::, you are referring to the class itself. Obviously the developers of PHP thought this difference was important enough to warrant having a separate syntax for each. Commented Jul 31, 2010 at 2:42
  • 1
    @musicfreak: One could still explicitly type $this-> even if it weren't mandatory, thus no feature would have been removed. Commented Jul 31, 2010 at 16:21
  • @konforce: True, but that would become confusing and error-prone. Also, it doesn't really help with functions and self::. (How do you call a global function with the same name as a method?) Commented Aug 1, 2010 at 2:05