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? 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
"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;
}
}
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 <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
"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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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? ...
|
by: FrancisC |
last post by:
#include <stdio.h>
int file_copy( char *oldname, char *newname );
int main()
{
char source, destination;
printf("\nEnter source file: ");
|
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...
|
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
|
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 = "";
}
|
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>
>...
|
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...
|
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...
|
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
|
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,...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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: 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,...
|
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...
|
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,...
|
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...
| |