Page 1 of 1

Stop script breaking when importing broken class/script file

Posted: Mon Jun 28, 2010 5:32 am
by terrypears
My first post and it's a question, as always :D

I'm in the process of building an admin framework where PHP class files are dynamically loaded and run when requested, with lots of use of class_exists(), method_exists(), call_user_func() to make sure the class itself conforms to the overall design.

The thing I've been banging my head on a brick wall with is finding a way to gracefully continue on from using an include() or include_once() when the class file (or script for that matter) is broken (i.e. someone else is editing it or there is a syntax error, etc). I've been through most of the php.net pages and the comments that suggest workarounds on this very issue but they don't work either. I've tried the whole include() within a try {}, an if/else on the include while error_reporting turned is off... I was going to try and do a get_file_contents() and run that through an eval or start playing with the set_error_handler but I don't think they'll work.

My only other thought is to try and validate the PHP file before loading, either way does anyone here know of a simple way of solving this? It would be brilliant if there was a function that would tell you if a php file was valid before loading, or give you a proper return on that failed include.

Re: Stop script breaking when importing broken class/script

Posted: Mon Jun 28, 2010 9:44 am
by AbraCadaver
You would need the runkit extension: http://php.net/manual/en/function.runkit-lint.php

But why would you include files that have syntax errors or other problems? If someone else edits the file and does damage then you need to fix it. This shouldn't be happening in production anyway. If this is a collaboration, then you need some version control. If someone breaks it with a change then revert the change. If this happens too often, kick them off the team.

Re: Stop script breaking when importing broken class/script

Posted: Mon Jun 28, 2010 10:16 am
by terrypears
Hey AbraCadaver,

That function looks, interesting :-) Shall have a play later.

As for why I want this, it's nothing to do with the rest of the team, although we do want to occasionally kill each other with various coding styles, etc :banghead: :-D

This has been something I wanted to be able to cater around for a while, so that if a library file wasn't correct then the script can carry on and log into the system that it's wrong. Lets say my style of coding is to potentially treat most incoming data as potential garbage and deal with it cleanly, even if it's my own code that is garbage :-)

Cheers for that, I shall post back on how it goes.