You are here: Re: avoid object creation... « PHP Programming Language « IT news, forums, messages
Re: avoid object creation...

Posted by Richard Levasseur on 02/16/06 04:26

You can throw an exception inside the constructor.
try {
$ob = new obj();
} catch(exception $e) { unset($ob) }

if(.....) { }

Though, I remember reading about memory leaks throwing an exception in
a constructor awhile back. When or if it that was fixed, I don't
recall. How it would deconstruct afterwards I don't know, either.

Better form would be to: use a factory or singleton pattern, $conn =
DB::getConnection();
a static method of the object, $conn = new DBConn($dsn);
if($conn->connect()) { ... }
or check some part of the object after created, $conn = new DBConn();
if($conn->isValid()) { .... }

Aside from out of memory errors or something beyond the object's
control, construction should never fail.

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация