Jump to content

Try Catch


The Little Guy

Recommended Posts

I added this to one of my methods:

try{
$this->hooksFired++;
call_user_func(array($this->$hook, $method));
}catch($e){ // Line 32

}

 

and I am getting this error:

 

Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING or T_NAMESPACE or T_NS_SEPARATOR in C:\Users\Ryan\Documents\NetBeansProjects\cleepcms\cleepcms\classes\Hook.php on line 32

 

I am not sure what is wrong, anyone know why I am getting that error?

Link to comment
https://forums.phpfreaks.com/topic/266148-try-catch/
Share on other sites

That would trigger a parse error as soon as the file is loaded. I don't remember for sure but you might be able to trap the error with set_error_handler() and interrupt the script however you see fit (such as by throwing an exception). Maybe. If not then you'd have to run the file through php -l and look for complaints.

 

Or you could just let the error happen. It's a parse error after all. It should never, ever happen in real code.

Link to comment
https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1363907
Share on other sites

call_user_func requires a callback as its argument. Why on earth do you put an array there?

 

In the case of an object, you use an array. The first value being a reference to the object, the second value being the method name

 

From the manual entry, Example 4

<?php

class myclass {
    static function say_hello()
    {
        echo "Hello!\n";
    }
}

$classname = "myclass";

call_user_func(array($classname, 'say_hello'));
call_user_func($classname .'::say_hello'); // As of 5.2.3

$myobject = new myclass();

call_user_func(array($myobject, 'say_hello'));

?>

Link to comment
https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364071
Share on other sites

That would trigger a parse error as soon as the file is loaded. I don't remember for sure but you might be able to trap the error with set_error_handler() and interrupt the script however you see fit (such as by throwing an exception). Maybe. If not then you'd have to run the file through php -l and look for complaints.

 

Or you could just let the error happen. It's a parse error after all. It should never, ever happen in real code.

 

set_error_handler doesn't catch these errors:

E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING

 

So the only thing I could also think of was doing exec("php -l filename.php");.

 

But I feel it would be better not to if possible.

Link to comment
https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364105
Share on other sites

Parse errors shouldn't happen outside of development.

 

I am making a CMS, and you can create plugins for it right within the CMS, and if while developing a plugin you have a syntax error it can bring the entire system to a halt making it so you can no longer edit the file unless you edit it externally.

 

Or maybe you download a plugin that was poorly coded, that could bring the server to a halt too.

Link to comment
https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364114
Share on other sites

set_error_handler doesn't catch these errors:

E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING

I vaguely remember a way that you could catch one/some of those, in some special circumstance probably under your control. Of course I don't remember how.

Link to comment
https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364122
Share on other sites

Syntax highlighting should be helpful. Regardless, you've got your mind made up, so best of luck. Again, eval will help you weed out fatal errors.

 

<?php

ob_start();
$result = eval('blah');
$response = ob_get_clean();
if( $result === FALSE ) {
if( stristr($response, 'parse error') !== FALSE )
	echo 'We found a parse error';
}

?>

Link to comment
https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364133
Share on other sites

You can use either register_shutdown_function or ob_start to register a function that will handle the resulting output. With error_get_last you can retrieve the last occured error (fatal error). I've used this on a few occasions with success. I always use register_shutdown_function in combination with error_get_last.

Link to comment
https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364198
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.