is there a way to catch parse errors in included files?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
compound_eye
Forum Newbie
Posts: 15
Joined: Wed Mar 17, 2004 8:42 pm

is there a way to catch parse errors in included files?

Post by compound_eye »

is there a way to catch parse errors in included files?

is there a way that i can include a file and know whether it worked nicely?

i have a function that loads custom classes for different purposes
and i'd like something like this
(but not actually this because i don't believe include() actually returns anything that is useful to me)

Code: Select all

$file = "giantmonkeys.php";

if(file_exists($file))
{
    if (include("$file"))
    {
        echo "<br>it worked!";
        // then do some thing useful with the functions and classes
        //  included in the file
    &#125;
   else
   &#123;
       echo "<br>hmm.. something seems to have gone wrong";
       //gracefully continue on without included functions and classes

   &#125;
&#125;
if the include does not work i dont want my program to crash, just gracefully not use the class defined in the included file

any one got any suggestions?

cheers

mathew
User avatar
Slippy
Forum Contributor
Posts: 113
Joined: Sat Jul 12, 2003 11:31 pm
Location: Vancouver eh!

Post by Slippy »

You could try using the isset() function or the is_callable function...

Another approach could be to use the function_exists() function...

Hope that helps :)
compound_eye
Forum Newbie
Posts: 15
Joined: Wed Mar 17, 2004 8:42 pm

Post by compound_eye »

yes, that would be helpful,
i am still curious if there is a way to catch a parsing error but
really the class_exists and is_callable will actually solve my problems as far as this project goes,

cheers

Mathew
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: is there a way to catch parse errors in included files?

Post by Weirdan »

compound_eye wrote: any one got any suggestions?
Try this:

Code: Select all

echo "<pre>";
$files=array(
        "q.php", // file with no syntax errors
        "asdasd.php", // file doesn't exist
        __FILE__, // this file
        "b.php" // this file contains syntax errors
        );
foreach ($files as $file){
    $output=array();
    exec("php -l $file", $output, $retval);
    echo "File: $file\n";
    var_dump($retval,$output);
}
On my box it gives me the following output:

Code: Select all

File: q.php
int(0)
array(1) &#123;
  &#1111;0]=&gt;
  string(34) "No syntax errors detected in q.php"
&#125;
File: asdasd.php
int(1)
array(1) &#123;
  &#1111;0]=&gt;
  string(38) "Could not open input file: asdasd.php."
&#125;
File: /home/weirdan/tests/a.php
int(0)
array(1) &#123;
  &#1111;0]=&gt;
  string(48) "No syntax errors detected in /home/weirdan/tests/a.php"
&#125;
PHP Parse error:  parse error in b.php on line 2
File: b.php
int(255)
array(3) &#123;
  &#1111;0]=&gt;
  string(0) ""
  &#1111;1]=&gt;
  string(43) "Parse error: parse error in b.php on line 2"
  &#1111;2]=&gt;
  string(20) "Errors parsing b.php"
&#125;
Post Reply