-3

PHP's include and require functions occur at runtime.

Consider this script, page1.php:

<?php
require('bootstrap.php');

If bootstrap.php does not exist, we get a runtime exception, e.g. "500 Internal Server Error".

How can we include bootstrap.php into page1.php such that they are treated as one unit instead of two separate units mungled together at run-time?

Is there any way to achieve "compile time" includes in PHP?

Pacerier
  • 86,231
  • 106
  • 366
  • 634
  • It looks like you are trying to tackle this problem the wrong way. Can't you just `require` the file in the bootstrap file of your project? Otherwise I think you may want to look into a custom error handler. – PeeHaa Jul 13 '13 at 15:19
  • A bootstrap file is a file which always loaded as the first thing in your file chain. This can then be used to load required script and-or setup the run environment. Basically all requests get routed through this file and after that it decides what other files to load next. – PeeHaa Jul 13 '13 at 15:22
  • @PeeHaa, Yes, we're both talking about this "bootstrap file". There's no guarantee that this bootstrap file exists on the file system. The question is asking how can we sorta "compile time" include this bootstrap file? – Pacerier Oct 29 '14 at 16:55
  • @Downvoters, [downvote with comments](http://meta.stackoverflow.com/a/252910/632951) if you think the question needs improving. – Pacerier Oct 29 '14 at 23:53
  • 1
    [People are already encouraged to post comments on downvotes](http://meta.stackexchange.com/a/2373/155197). And people not commenting on downvotes might just have a good reason for not doing hence the reason it is not mandatory. So imho asking for comments on downvotes just introduces noise. – PeeHaa Oct 30 '14 at 09:45
  • @PeeHaa, It was a good attempt to get people to explain their downvotes, but imho it's not working because when there's too many low quality questions and one-shot posters who don't bother coming back, people grew too lazy to bother typing a reason. Asking for comments on downvotes isn't noise, but acts as an additional prompt to the downvoter to let him know this isn't a one-shot I-don't-care question and his comment will well be perused and **acted upon**. – Pacerier Oct 30 '14 at 09:58

3 Answers3

0

This is wrong assumption of yours.

Neither 200 nor 404 have to be sent in case of error but 503.

It is said that modern PHP versions have to take care of it, but I have some reports that it doesn't.
The only guaranteed way known to me is to use php-fpm, as sending 503 in case of error is one its core features.

Anyway, at least try to set display_errors = off in PHP settings (ini or perdir) and see. there should be no error message (as it ought to be on a live server) but 5xx status which will tell a client that page exists but experience temporary problems.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • Ok it is debatable whether we should return 404 or 503. But this is not the point.... How do we emulate "compile time" includes in PHP? – Pacerier Jul 13 '13 at 15:19
  • 1
    Oh, no. It is not debatable at all. – Your Common Sense Jul 13 '13 at 15:23
  • Ok then we do not debate it. How do we emulate "compile time" includes in PHP? – Pacerier Jul 13 '13 at 15:25
  • Regarding your edit, I would say I don't think you understood the question (see above)... I do not want to tell the client the page exist. I want to tell the client the page doesn't exist. – Pacerier Jul 13 '13 at 15:39
  • Yep. That's what you are doing wrong. You may want 404 only if included file contains *data*, not *code*. Yet it haven't to be an include() at all in this latter case. – Your Common Sense Jul 13 '13 at 15:41
  • If bootstrap.php does not exist, I *want* the server to respond as though main.php did not exist. Please explain how is this interface wrong. – Pacerier Jul 13 '13 at 15:52
  • Why do you want it? What's so special with your application that it don't have to follow an industrial standard? – Your Common Sense Jul 13 '13 at 16:03
  • http://tools.ietf.org/html/rfc2616#section-10.4.5 *The server has not found anything matching the Request-URI. No indication is given of whether the condition is temporary or permanent. The 410 (Gone) status code SHOULD be used if the server knows, through some internally configurable mechanism, that an old resource is permanently unavailable and has no forwarding address. This status code is commonly used when the server does not wish to reveal exactly why the request has been refused, or when no other response is applicable.* – Pacerier Jul 13 '13 at 16:09
  • PHP files usually contains code, not data. While this "search anything matching the Request-URI." apparently belongs to data. – Your Common Sense Jul 13 '13 at 16:15
  • **and my PHP files contain code, not data.** Why do you say "search anything matching the Request-URI" apparently belongs to data when it apparently does not "apparently belong to data"? – Pacerier Jul 13 '13 at 16:20
  • because Request-URI is used to identify a page with data. – Your Common Sense Jul 13 '13 at 16:22
  • I wonder who is the one doing assumptions now. http://tools.ietf.org/html/rfc2616#section-5.1.2 *The Request-URI is a Uniform Resource Identifier and identifies the **resource** upon which to apply the request.* http://tools.ietf.org/html/rfc2616#section-3.2 *As far as HTTP is concerned, Uniform Resource Identifiers are simply formatted strings which identify--via name, location, or any other characteristic--**a resource**.* – Pacerier Jul 13 '13 at 16:28
  • http://tools.ietf.org/html/rfc2616#section-10.4.5 This status code (404) is commonly used when the server does not *wish* to reveal exactly why the request has been refused, or when no other response is applicable. – Pacerier Jul 13 '13 at 16:30
  • You still fail to provide a reason, why *you* want to make false 404 instead of proper 503 and what's so special with your site that it doesn't follow an industry standard. – Your Common Sense Jul 13 '13 at 16:47
  • You should be aware that **the** industry standard doesn't mean *your* standard. My 404 is a true 404 as defined by the industry standard http://tools.ietf.org/html/rfc2616#section-10.4.5 . Do read the links I've provided you with if you wish to understand what "industry standards" mean. – Pacerier Oct 29 '14 at 15:44
0

If you really want to do this, then it would be easier to just invert the problem using URL rewriting. Instead of making main.php and all your other scripts include bootstrap.php, just have a third script include bootstrap.php and the others.

You can keep your existing URLs and use rewrite rules to route all requests to the affected files through this central script. This script would do your include/error check of bootstrap.php and then either send the 404 response or figure out the original script path based on the request URI and include it. That would allow you to do all your custom error handling in one place.

Pacerier
  • 86,231
  • 106
  • 366
  • 634
Peter Geer
  • 1,132
  • 1
  • 6
  • 11
  • It all depends on what you're trying to accomplish and why bootstrap.php would be missing. If main.php is the request entry-point, then your options are pretty limited. Another option would be to put your error checks in a file that's loaded using the auto_prepend_file directive, but then you'd have to guarantee that *that* file always exists. I'm not sure whether or not that's an improvement. – Peter Geer Jul 13 '13 at 18:03
0

(Note: answering my own question as a proper answer had not appeared after several months.)

True compile-time includes does not exist in "native" PHP because the very essence of PHP is an interpreted / runtime scripting language.

PHP accelerators may give the illusion of compile-time includes, but they actually track the state of each included file and redo an include at runtime if the file is edited. Which means that we'll still get a runtime error/warning if the included file is deleted, or if it doesn't even exist in the first place.

However, what's wanted can be accomplished using a PHP preprocessor which replaces all your include statements with their actual file content. This will work as long as the name of the included files are not dynamic strings and can be statically determined. (It's not too tough to write a custom preprocessor if your use cases are simple — e.g. no usage of namespaces etc).

If all that's wanted is to return a 404 page when an include statement fails, then look for an option provided by the web server. If it doesn't allow such a configuration, the only fall back would be URL rewriting solution as provided by Peter's answer.

Community
  • 1
  • 1
Pacerier
  • 86,231
  • 106
  • 366
  • 634