NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP 5 Throwing Exceptions in Constructors

Tim Gales tgales at tgaconnect.com
Thu Feb 10 07:24:05 EST 2005


Ophir Prusak writes:

> I was dealing with this exact same dilemma on a project I'm 
> currently coding. We ending up going with Adam's option B: 
> move the code which can fail to outside the constructor.
> 
> In the original design the you could create an object where 
> the constructor could read some information from a database.
> 
> In the final design, we moved the database code to a new 
> load() function that executes the actual database call. This 
> does add an additional step, but having the constructor throw 
> an execution just messes things up.

"Factory methods are sometimes a more flexible way to instantiate 
a class than directly calling the constructor of the class, for 
the following reasons:

Unlike constructors, factory methods are not required to create a 
new object each time they are invoked...
 
from 'The Factory Design Pattern' by  Amit Goel
http://www.ondotnet.com/pub/a/dotnet/2003/08/11/factorypattern.html 

In the article Amit goes on to point out that objects returned by a
factory 
"need not even exist at the time the factory method is written."

(the article draws on examples from the .NET Framework Class Library 
to explain the potential advantage of using a factory pattern) 

T. Gales & Associates
'Helping People Connect with Technology'

http://www.tgaconnect.com

 




More information about the talk mailing list