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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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? |
|
|||
|
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 |
|
|||
|
"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; } } |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
"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. |