?
Solved

Catching a failed require_once that gets page from a function

Posted on 2008-10-10
2
Medium Priority
?
161 Views
Last Modified: 2008-10-14
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
Comment
Question by:dban00b
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 2000 total points
ID: 22691778
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
 
LVL 1

Author Comment

by:dban00b
ID: 22711907
Those two extra functions do indeed catch it! Thanks.
0

Featured Post

WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question