Programming Theory
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Codewalkers ForumsOther TechnologiesProgramming Theory

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Codewalkers Forums Sponsor:
  #1  
Old December 31st, 2003, 03:56 AM
bluephoenix's Avatar
bluephoenix bluephoenix is offline
Levelheaded Curmudgeon
Codewalkers Novice (500 - 999 posts)
 
Join Date: Apr 2007
Location: Syracuse, NY
Posts: 508 bluephoenix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 24 m 56 sec
Reputation Power: 3
Send a message via AIM to bluephoenix
:: 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

Reply With Quote
  #2  
Old December 31st, 2003, 04:21 AM
Matt Matt is offline
Contributing User
Codewalkers Specialist (4000 - 4499 posts)
 
Join Date: Apr 2007
Location: Florida
Posts: 4,158 Matt User rank is Private First Class (20 - 50 Reputation Level)Matt User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 4 h 12 m 16 sec
Reputation Power: 7
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......

Reply With Quote
  #3  
Old January 3rd, 2004, 07:10 AM
bluephoenix's Avatar
bluephoenix bluephoenix is offline
Levelheaded Curmudgeon
Codewalkers Novice (500 - 999 posts)
 
Join Date: Apr 2007
Location: Syracuse, NY
Posts: 508 bluephoenix User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 24 m 56 sec
Reputation Power: 3
Send a message via AIM to bluephoenix
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:B and play around with some examples. Thanks, Matt.


-Tim

Reply With Quote
  #4  
Old January 3rd, 2004, 05:01 PM
honcho's Avatar
honcho honcho is offline
Contributing User
Codewalkers Beginner (1000 - 1499 posts)
 
Join Date: Apr 2007
Location: Cape Cod
Posts: 1,347 honcho User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 3 h 52 m 2 sec
Reputation Power: 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.

Reply With Quote
Reply

Viewing: Codewalkers ForumsOther TechnologiesProgramming Theory > :: vs ->


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek