views:

929

answers:

2

I am trying to better understand basic concepts in OOP. What are static and dynamic variables and methods in object-oriented programming? What is, for instance, the difference between using $this vs. double colon (::)?

$this ($this->a_method())

  • Advantages: ?.
  • Disadvantages: ? ... "this" is not self-documenting as in: $this->method_from_an_extended_class().

Double colon (someclass::a_method())

  • Advantages: ?
  • Disadvantages: ?
+1  A: 

Quoting the static page in the PHP manual :

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).

Also note that when declaring a property as static, there will be only one version of that property for the whole script : static methods/properties "act at the class level rather than at the instance level" (quoting wikipedia).


About advantages / disadvantages, it's a bit hard to answer clearly, as those are just not the same...

The question is : what do you need ?

If you have an object that contains data, and want methods to deal with that data, you'll use dynamic properties and methods.

On the other hand, if you just want to use a class as container for methods that deal with external data (ie, not internal to the class), you'll probably use static methods.
For instance, I am sometimes using static methods as wrappers to libraries that don't export an Oriented-Object API : it allows me to call one class::method, instead of several functions.


Some would say an "advantage" of static methods is that you don't need to instanciate the class to use them -- that is true ; but it also means that you don't get objects, with methods working on them ; well : as I said, using static or not depends on what you need.

Oh, I almost forgot : if you are willing to use unit-testing, you might find out that static methods are not easy to test ; for instance, see Static Methods are Death to Testability.

And static properties (same with singleton and registry design patterns, which rely on static properties/methods) kind of imply "global state", like "global variables" -- which some don't quite like ^^

Pascal MARTIN
+3  A: 

"Static" and "dynamic" aren't the right descriptions for that.

-> indicates a instance functions or instance data, meaning that the function or data has an implicit $this reference. To put it another way you're referring to the function or variable within a particular object.

:: indicates a class function or class variable. This is very similar to a global function or variable in that there is no implicit $this reference. All instance of that class share that function or variable.

"Dynamic" would be a more accurate description for, say, PHP overloading where you can "dynamically" create variables, for example, using the magic methods __get() and __set() (which are called when you try to access a property that can't be found; you can overload these methods to essentially pretend the requested member exists).

cletus