note 102511 added to function.defined

[email protected] Fri, 18 Feb 2011 03:52:14 -0800
Newsgroups php.notes
Message-ID <[email protected]>
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.
----
Server IP: 87.124.126.117
Probable Submitter: 87.124.34.68 (proxied: 87.124.34.68)
----
Manual Page -- http://www.php.net/manual/en/function.defined.php
Edit        -- https://master.php.net/note/edit/102511
Del: integrated  -- https://master.php.net/note/delete/102511/integrated
Del: useless     -- https://master.php.net/note/delete/102511/useless
Del: bad code    -- https://master.php.net/note/delete/102511/bad+code
Del: spam        -- https://master.php.net/note/delete/102511/spam
Del: non-english -- https://master.php.net/note/delete/102511/non-english
Del: in docs     -- https://master.php.net/note/delete/102511/in+docs
Del: other reasons-- https://master.php.net/note/delete/102511
Reject      -- https://master.php.net/note/reject/102511
Search      -- https://master.php.net/manage/user-notes.php