473,387 Members | 1,687 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,387 software developers and data experts.

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?

Jul 17 '05 #1
5 3477
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
Jul 17 '05 #2
"thecrow" <ca**********@hotmail.com> wrote in message
news:11**********************@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;
}
}

Jul 17 '05 #3
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.

Jul 17 '05 #4
In article <11*********************@g14g2000cwa.googlegroups. com>,
"thecrow" <ca**********@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
Jul 17 '05 #5
"thecrow" <ca**********@hotmail.com> wrote in message
news:11*********************@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.
Jul 17 '05 #6

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

Similar topics

3
by: Pierre Rouleau | last post by:
The std::exception class defined in the Standard C++ <exception> header specifies that the constructors could throw any exception becuase they do not have a throw() specification. Why is that? ...
23
by: FrancisC | last post by:
#include <stdio.h> int file_copy( char *oldname, char *newname ); int main() { char source, destination; printf("\nEnter source file: ");
20
by: TTroy | last post by:
Hello, I have found some peculiar behaviour in the fgets runtime library function for my compiler/OS/platform (Dev C++/XP/P4) - making a C console program (which runs in a CMD.exe shell). The...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
4
by: Lenn | last post by:
I have the following method: public void Execute(TCPCommand command, out string outputResponse, out string error) { //snipped code outputResponse = "some message"; error = ""; }
2
by: Marc Jennings | last post by:
Hi there, I have been given a rather poor schema for an XML file in the following format : ><item> > <key>ProductCode</key> > <value>1234-5678</value> > <key>Description</key> >...
2
by: learningonmyown | last post by:
Hello, I am writing a script that needs to get a numeric value from the keyboard. I have already solved the problem of when a user inputs a string, so that is not the problem! Now I need to solve...
13
by: Protoman | last post by:
I'm getting an error: 10 C:\Dev-Cpp\Enigma.cpp no match for 'operator<' in 'i < (+cleartext)->std::basic_string<_CharT, _Traits, _Alloc>::end ()' Code: Enigma.hpp...
3
by: priyamurthy | last post by:
Do constructors in cpp has any return type? If so please tell clearly. Do dynamic constructors in cpp has any return type? If so please explain about that
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: 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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.