473,382 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

avoid object creation...

exist a way to avoid an object creation on error??
i have this on php5.0.5

public function __construct(){
@list($this->DSN,
$this->DBuser,
$this->DBpass) = func_get_args();

$this->res =
odbc_connect($this->DSN,$this->DBuser,$this->DBpas$
if(!is_resource($this->res)){
echo odbc_error()."<br>\n".odbc_errormsg();
/// here put something to avoid contruction of the
object
}
}

in the script i have this:

$db = new DbCon('dsn','user','pass');
if(is_object($db)){
//do some
}else{
//do some
}

Feb 15 '06 #1
1 1279
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.

Feb 16 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Erick | last post by:
Hi, Does any one know how to avoid to register of the event log source by the TransactedInstaller object? I have a service which have some code into the main method to self-register base on...
6
by: | last post by:
I have a class with overloading operator new. (Because, if an identical object exists, return a pointer to existed object instead of a new pointer) It has no sense (it is dangerous) to allocate an...
54
by: tshad | last post by:
I have a function: function SalaryDisplay(me) { var salaryMinLabel = document.getElementById("SalaryMin"); salaryMinLabel.value = 200; alert("after setting salaryMinLabel = " +...
8
by: Anthony Munter | last post by:
I have a web application with impersonate=”true” in Web.config and on my own logon page I allow the user to either - specify a userid/password for the app to impersonate when calling legacy...
3
by: Nick Dreyer | last post by:
I was quite surprised to notice that Sub New() gets called twice, once at declaration time and once at creation time. I can't figure out why it would be called at declaration if there is no class...
11
by: Kevin Prichard | last post by:
Hi all, I've recently been following the object-oriented techiques discussed here and have been testing them for use in a web application. There is problem that I'd like to discuss with you...
2
by: Angel Of Death | last post by:
I have a method. It takes some XML as a parameter. Depending on the content of the XML it should create a specific object and call a KNOWN method. So: public void PersistXml(string XmlData){} ...
4
by: alex | last post by:
I am so confused with these three concept,who can explained it?thanks so much? e.g. var f= new Function("x", "y", "return x * y"); function f(x,y){ return x*y } var f=function(x,y){
6
gpraghuram
by: gpraghuram | last post by:
HI All, Someone asked me the following question and i am not able to get an answer for this. In c++ how will you restrict 1)object creation in heap 2)object creation in stack Can anyoine...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.