TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   how to capture and show error in oop php to user? (http://www.talkphp.com/advanced-php-programming/3399-how-capture-show-error-oop-php-user.html)

kokjj87 10-01-2008 12:44 PM

how to capture and show error in oop php to user?
 
hi, i am new to oop php programming style.. i am trying to write a image class to process images, but how do i capture the error(for example when you upload a non image file) and show it to the user?

PHP Code:

my image class
----------------------------------------------------
<?php
class image
{
private 
$allowextension = array("image/gif","image/jpeg","image/png");
private 
$source;

        function 
__construct($source)
        {
        
$this->source unserialize($source);

                
//check whether the upload file is the extension that is allow
                
if(!in_array(($this->source['type']), $this->allowextension))
                {
                  
#how do i show an error to the user?.. in a presentable way
                
return false;
                }
        }
}

?>

PHP Code:

my form
---------------------------------------------------
<?php
include("imageClass.php");


if(isset(
$_FILES["file"]) && !empty($_FILES["file"]))
{
$upload serialize($_FILES["file"]);
$image = new image("$upload");
}

else
{
?>
<form method="post" enctype="multipart/form-data" action="<?php echo
$_SERVER['PHP_SELF']; ?>" >
<input type="file" name="file" /><input type="submit" value="Upload"/>
</form>

<?php ?>

I am looking at try and catch but couldn't understand how to show it to the user in a more customizable way(can be use with css).
Thanks in advance!

Kalle 10-02-2008 03:51 AM

Your code is sort of wrong, you cannot return a value in a contructor or destructor, you can only catch these sort of values if they are staticlly declared, but non of the magic methods may be declared staticlly.

To detect an error you should use Exceptions, instead of the return false part in your constructor you say:
PHP Code:

throw new Exception('<error message goes here>'); 

And you replace this part:
PHP Code:

$image = new image("$upload"); 

With this to catch the exception:
PHP Code:

try
{
    
$image = new image($upload);
}
catch(
Exception $e)
{
    echo 
$e->getMessage();


You can also use a default exception handler using the set_exception_handler() function.

For more information about the methods that the Exception class exposes, then referer to the php.net docs here:
PHP: Exception - Manual

kokjj87 10-02-2008 08:45 AM

Thank you Kally.

this is what i am looking for.. =)

can you give an idea of how to use the capture message from the exception and display it to the user with style(css).

Kalle 10-02-2008 12:13 PM

Use the getMessage() from the exception class to print the error message set from the constructor

kokjj87 10-02-2008 04:31 PM

what about this, let say that i have caught a few error and i only want to show some of it(those that i want to show, those that would not lead to security issues), and hide the rest..
How do you do it?

Wildhoney 10-02-2008 06:37 PM

It sounds as though you may wish to use error handling, instead of exception handling, to me.


All times are GMT. The time now is 08:57 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0