On Nov 3, 1:34*pm, Cyphos <mweich...@gmail.comwrote:
Hello,
I'm late getting to the PHP scene, but I thought I'd start to get into
it. I have a .NET and Ruby background, therefore I'm an OOP guy.
One thing that I haven't figured out to do is turn on Exception
handling for PHP errors. I'm used to exceptions, and noticed that PHP
5 has support for exceptions, so I'd like all errors thrown as
exceptions.
If anyone could help me with that, I'd very much appreciate it.
Thanks,
Mike
Exceptions are a relatively new addition to PHP, they didn't exist as
a concept in PHP 4. PHP 4 would instead emit an error message and
continue or abort depending on the error's severity. For backwards
compatibility reasons, any function that existed in PHP 4 still
behaves like this in PHP 5.
You can write wrapper functions yourself for whatever you need to
throw an exception. For example:
function myFopen ($path, $mode, $use_include_path = false, $context =
NULL)
{
if ($handle = fopen ($path, $mode, $use_include_path, $context))
{
return ($handle);
}
else
{
throw new Exception ('Unable to open file ' . $path);
}
}
You can then use your own function where you would use fOpen inside a
try block and put whatever error recovery code you feel you need in
the catch block.
The fopen call inside the function will of course still cause an error
message to be emitted. You can get around this by either turning
error reporting off or by using the @ operator to suppress the error.
Both have their benefits and drawbacks. Turning off error reporting
has no overhead and in a production environment is recommended anyway
because it prevents information that might be useful to hackers being
emitted by malfunctioning scripts. However in a test environment it
turns off all error reporting and hinders debugging as a result. The
@ operator can be used on a per call basis, but it has considerable
overhead and can really slow a PHP script down if used incautiously
(in a loop for example).