The Little Guy Posted July 24, 2012 Share Posted July 24, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/ Share on other sites More sharing options...
The Little Guy Posted July 24, 2012 Author Share Posted July 24, 2012 I got it, but it didn't seem to do what I wanted. I am looking to skip calling call_user_func(array($this->$hook, $method)); if the calling function has an error in it, such as a missing simi-colon. Any way I can do that? Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1363893 Share on other sites More sharing options...
requinix Posted July 24, 2012 Share Posted July 24, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1363907 Share on other sites More sharing options...
peipst9lker Posted July 24, 2012 Share Posted July 24, 2012 You need to provide an identifier in catch-blocks e.g try { $this->hooksFired++; call_user_func(array($this->$hook, $method)); } catch (Exception $e) { // error handling code } Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1363935 Share on other sites More sharing options...
silkfire Posted July 24, 2012 Share Posted July 24, 2012 call_user_func requires a callback as its argument. Why on earth do you put an array there? Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364066 Share on other sites More sharing options...
xyph Posted July 24, 2012 Share Posted July 24, 2012 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')); ?> Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364071 Share on other sites More sharing options...
The Little Guy Posted July 24, 2012 Author Share Posted July 24, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364105 Share on other sites More sharing options...
xyph Posted July 24, 2012 Share Posted July 24, 2012 Parse errors shouldn't happen outside of development. Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364110 Share on other sites More sharing options...
The Little Guy Posted July 24, 2012 Author Share Posted July 24, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364114 Share on other sites More sharing options...
xyph Posted July 24, 2012 Share Posted July 24, 2012 In this case, run the code through eval. A parse error will result in FALSE. On another note, there are so many things wrong with allowing developers to code PHP-based-plugins directly in the CMS. Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364115 Share on other sites More sharing options...
The Little Guy Posted July 24, 2012 Author Share Posted July 24, 2012 On another note, there are so many things wrong with allowing developers to code PHP-based-plugins directly in the CMS. Such as what? Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364116 Share on other sites More sharing options...
requinix Posted July 24, 2012 Share Posted July 24, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364122 Share on other sites More sharing options...
xyph Posted July 24, 2012 Share Posted July 24, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364123 Share on other sites More sharing options...
The Little Guy Posted July 24, 2012 Author Share Posted July 24, 2012 I am using http://codemirror.net for my "IDE", and if you need more, that what my "IDE" offers, you do have the ability to code in your own editor if you wish. Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364126 Share on other sites More sharing options...
xyph Posted July 24, 2012 Share Posted July 24, 2012 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'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364133 Share on other sites More sharing options...
The Little Guy Posted July 25, 2012 Author Share Posted July 25, 2012 but wont eval actually run the code? Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364143 Share on other sites More sharing options...
xyph Posted July 25, 2012 Share Posted July 25, 2012 It sure will... Perhaps you want to set up some sort of client with xdebug and DBGp? Or write your own parser? Or simply have your developers code plug-ins in a development environment? Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364148 Share on other sites More sharing options...
ignace Posted July 25, 2012 Share Posted July 25, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364198 Share on other sites More sharing options...
The Little Guy Posted July 25, 2012 Author Share Posted July 25, 2012 doesn't register_shutdown_function exit the current script once the function is done though? If that is the case I can't have that. I added a lint test which seems to work I will then take the errors from that and save them to a custom error logging system. Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364273 Share on other sites More sharing options...
ignace Posted July 25, 2012 Share Posted July 25, 2012 You can do something like: register_shutdown_function(function() { if ($error = error_get_last()) { header('Location: error.php?ser=' . urlencode(serialize($error))); } }); Quote Link to comment https://forums.phpfreaks.com/topic/266148-try-catch/#findComment-1364300 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.