note 102511 deleted from function.defined by danbrown
| From: | danbrown@php.net | Date: | Fri, 18 Feb 2011 13:30:10 +0000 |
| Subject: | note 102511 deleted from function.defined by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-177111@lists.php.net to get a copy of this message | ||
Note Submitter: mattward41 at gmail dot com
----
If you wish to protect files from being called directly, I've found in tests that calling an
empty function is quicker than calling defined() on a constant (as certain frameworks and PHP
applications do). Therefore you can declare a uniquely-named empty function in your main script then
call it from your sub-scripts in place of defining and checking for a constant. For example:
Main script:
<?php
function my_unique_function() {}
require('subscript.php');
?>
Sub script:
<?php
my_unique_function();
?>
This will throw a fatal error if the sub script hasn't been called via the main script where
my_unique_function() is declared as the function does not exist and cannot be called.
In tests on the same hardware I've found calling an existing empty function about 3 times
quicker than checking if a constant is defined. See http://matthewjamesward.co.uk/php/function_defined.php
for details.