PHP - Does it ignore return val from constructors?

This is a discussion on PHP - Does it ignore return val from constructors? within the PHP Language forums, part of the PHP Programming Forums category; I am trying to figure out how to make an object creation fail for ease of error handling. Oddly, I ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-24-2005
thecrow
 
Posts: n/a
Default PHP - Does it ignore return val from constructors?

I am trying to figure out how to make an object creation fail for ease
of error handling. Oddly, I can't work out how to do it.

Here's a reduced code sample of how I hoped it would work, but it
doesn't:

class Thing {
var $error;
function Thing() {
return 0;
}
}

if (!$theThing = new Thing()) {
echo "Couldn't create the thing -" . $thing->error;
}

I guess I could set $this to false instead of returning 0. But then I
wouldn't get the benefit of the error message, unless I echoed it,
which I don't like to do in classes.

Any good way to do this?

Reply With Quote
  #2 (permalink)  
Old 02-24-2005
Marcin Dobrucki
 
Posts: n/a
Default Re: PHP - Does it ignore return val from constructors?

thecrow wrote:
> I am trying to figure out how to make an object creation fail for ease
> of error handling. Oddly, I can't work out how to do it.
>
> Here's a reduced code sample of how I hoped it would work, but it
> doesn't:
>
> class Thing {
> var $error;
> function Thing() {
> return 0;
> }
> }
>
> if (!$theThing = new Thing()) {
> echo "Couldn't create the thing -" . $thing->error;
> }
>
> I guess I could set $this to false instead of returning 0. But then I
> wouldn't get the benefit of the error message, unless I echoed it,
> which I don't like to do in classes.


I have a recollection that you can't get return values from
constructors. The way around it would be something like this:

class Foo {

function Foo () {
$this->initialize();
}

function initialize() {
if (yadayadayada) {
...
}
else {
return 0;
}
}
}

/Marcin
Reply With Quote
  #3 (permalink)  
Old 02-24-2005
Chung Leong
 
Posts: n/a
Default Re: PHP - Does it ignore return val from constructors?

"thecrow" <carltonbrown@hotmail.com> wrote in message
news:1109247316.134425.288660@z14g2000cwz.googlegr oups.com...
> I am trying to figure out how to make an object creation fail for ease
> of error handling. Oddly, I can't work out how to do it.
>
> Here's a reduced code sample of how I hoped it would work, but it
> doesn't:
>
> class Thing {
> var $error;
> function Thing() {
> return 0;
> }
> }
>
> if (!$theThing = new Thing()) {
> echo "Couldn't create the thing -" . $thing->error;
> }
>
> I guess I could set $this to false instead of returning 0. But then I
> wouldn't get the benefit of the error message, unless I echoed it,
> which I don't like to do in classes.
>
> Any good way to do this?
>


Doing what you're not supposed to:

class Thing {
var $error;
function Thing() {
$this = false;
}
}



Reply With Quote
  #4 (permalink)  
Old 02-25-2005
thecrow
 
Posts: n/a
Default Re: PHP - Does it ignore return val from constructors?

Thank you for the explanation.

That leaves me with the problem, if I fail the constructor by setting
$this to false, how do I get the error information without echoing it?

I guess I can go search on that, but if anyone has any commonly used
ideas I would appreciate it.

Reply With Quote
  #5 (permalink)  
Old 02-25-2005
Alessandro Ranellucci
 
Posts: n/a
Default Re: PHP - Does it ignore return val from constructors?

In article <1109294863.754779.98710@g14g2000cwa.googlegroups. com>,
"thecrow" <carltonbrown@hotmail.com> wrote:

> Thank you for the explanation.
>
> That leaves me with the problem, if I fail the constructor by setting
> $this to false, how do I get the error information without echoing it?
>
> I guess I can go search on that, but if anyone has any commonly used
> ideas I would appreciate it.


Use a manual constructor:

class Thing {
function Thing {
#nothing here
}
function fetch {
if ($CanInstantiate) {
return new Thing;
} else {
return false;
}
}
}

- Alessandro
Reply With Quote
  #6 (permalink)  
Old 02-26-2005
Chung Leong
 
Posts: n/a
Default Re: PHP - Does it ignore return val from constructors?

"thecrow" <carltonbrown@hotmail.com> wrote in message
news:1109294863.754779.98710@g14g2000cwa.googlegro ups.com...
> Thank you for the explanation.
>
> That leaves me with the problem, if I fail the constructor by setting
> $this to false, how do I get the error information without echoing it?
>
> I guess I can go search on that, but if anyone has any commonly used
> ideas I would appreciate it.
>


That was just a stupid PHP 4 trick. Don't use it. It doesn't work in PHP 5.

Constructors are meant for initializing an object. They are not supposed to
fail. The only reasonable way to handle a failure in a constructor is
through exception handling, which doesn't exist in PHP 4. As others have
suggested, put the code that could potentially fail in a separate function.


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 10:49 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0