|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stay one step ahead of the competition. Evaluate and give feedback
on some of the hottest web development tools on the market today.
Make your opinion heard! Click
Here
|
|
#1
|
||||
|
||||
|
:: vs ->
From my understanding, :: abstracts a method so you don't have to instantate an actual object.
require_once("class.bar.php"); $b = new bar(); $foo = $b->baz("x"); require_once("class.bar.php"); $foo = bar::baz("x"); I can see the obvious benefits of :: such as less memory consumpution, shorter code and cleaner line noise. But what are some of the other benefits? When would I be in a situation where I should chose :: over ->? Tim |
|
#2
|
|||
|
|||
|
RE: :: vs ->
[moved to theory.....]
One advantage is the ability to return a instance of the class, or a different class, depending upon the result of calling that function. For example, take the PEAR DB package, to initiate a connection you call: $db = DB::connect($dsn); then you check to see if the returned value is an instance of the error class: if (DB::isError($db)) { die ($db->getMessage()); } if it is not an instance of the error class, you know it is an instance of the DB class.... Just one example, I am sure there are others...... |
|
#3
|
||||
|
||||
|
RE: :: vs ->
Ah, I get it... the isError method is sort of like a recursive check on the object.
I'll have to look at PEAR: -Tim |
|
#4
|
||||
|
||||
|
RE: :: vs ->
You're really talking about two different programming models here. If you have an object that is a stateless collection of functions, then there's no need to instaniate an instance, just use :: to call the functions. However, if you have an object with state and the function you are calling on that object deals with that state (mutable or not), then there is no choice and you have to call it with ->.
Of course, some objects can be a combination of the above, but the functions you call with :: still must be stateless in terms of the object (i.e., static functions) and are probably only included in the object for convience, clarity, and to improve the abstraction. |
![]() |
| Viewing: Codewalkers Forums > Other Technologies > Programming Theory > :: vs -> |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|
|
|