• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 164
  • Last Modified:

Catching a failed require_once that gets page from a function

I'm working on a new function that builds my output page to separate my code from my content when I ran into a snag--my normal line of thinking doesn't work.

My cheap and easy was to deal with a failed require_once is to toss the user to the 404 like so:
      if(file_exists("../site/siteclass.php"))
            require_once("../site/siteclass.php");
      else
            header('Location: http://intranet/404.php');

But when my require_once is the output of a function rather than a file, my normal trick doesn't work:
      if(file_exists($SITE->displayoutput()))
            require_once($SITE->displayoutput());
      else
            header('Location: http://intranet/404.php');

The IF statement returns a fatal error when the $SITE->displayoutput() function doesn't exist.

Help my fried brain, how do I change my code to catch when require_once's target function fails/doesn't exist. I purposely have my singleton include commented out to force the failure so I can learn how to catch it.
<?php
	/* commented out to force the next step to fail
	   to learn how to catch the failure
		//Get the SITE Singleton
		if(file_exists("../site/siteclass.php"))
			require_once("../site/siteclass.php");
		else
			header('Location: http://intranet/404.php');
	*/
 
	//Get the output from the SITE Singleton
	if(file_exists($SITE->displayoutput()))
		require_once($SITE->displayoutput());
	else
		header('Location: http://intranet/404.php');
?>

Open in new window

0
dban00b
Asked:
dban00b
1 Solution
 
Roger BaklundCommented:
Try using isset() and method_exists(), something like this:

if(isset($SITE) and 
   method_exists($SITE,'displayoutput') and 
   file_exists($SITE->displayoutput()))
                require_once($SITE->displayoutput());
        else
                header('Location: http://intranet/404.php');

Open in new window

0
 
dban00bAuthor Commented:
Those two extra functions do indeed catch it! Thanks.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now