note 102511 deleted from function.defined by danbrown

[email protected] Fri, 18 Feb 2011 05:30:10 -0800
Newsgroups php.notes
Message-ID <[email protected]>
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.