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
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
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
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
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
Share on other sites

On another note, there are so many things wrong with allowing developers to code PHP-based-plugins directly in the CMS.

 

Such as what?

 

Lack of development environment, for one.

 

Are you coding some sort of IDE in your CMS?

Link to comment
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.