note 95107 added to function.include
| From: | randallgirard at hotmail dot com | Date: | Mon, 14 Dec 2009 08:23:56 +0000 |
| Subject: | note 95107 added to function.include | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-163915@lists.php.net to get a copy of this message | ||
Heres a nifty little include function I whipped up utilizing extract and compact to post variables
to and return from an include file (great for configurations, ex: DB connection info, or whatever
else you can imagine). I'm using namespaces and keeping my function/class names simple, so you
can rename the function to whatever you'd like.
<?php
# include function allowing variables to be posted to and returned from the target script
function inc( $__path, $__return='.', array $__post=array() ) {
# post var's to the local scope
if ( count( $__post ) )
extract($__post, EXTR_SKIP);
# include the file and store the result
if ( $__result = include $__path ) {
# return requested variables from the included file
if ( is_array($__return) )
$result = compact($__return);
# Return ALL variables defined from within the included file
# NOTE: $__post keys are NOT included!
else if ( $__return == '.' )
$result = compact( array_diff( array_keys(get_defined_vars()),
array('GLOBALS', '__path', '__return', '__post',
'__result')
+ array_keys($__post) ) );
# Is $__return a variable from the file?
else if ( $__return && isset($$__return) )
$result = array( $__return => $$__return );
else
$result = array();
# unshift the include result into $result
array_unshift($result, $__result);
return $result;
}
return array($__result);
}
?>
----
Server IP: 208.69.120.55
Probable Submitter: 76.167.60.11
----
Manual Page -- http://www.php.net/manual/en/function.include.php
Edit -- https://master.php.net/note/edit/95107
Del: integrated -- https://master.php.net/note/delete/95107/integrated
Del: useless -- https://master.php.net/note/delete/95107/useless
Del: bad code -- https://master.php.net/note/delete/95107/bad+code
Del: spam -- https://master.php.net/note/delete/95107/spam
Del: non-english -- https://master.php.net/note/delete/95107/non-english
Del: in docs -- https://master.php.net/note/delete/95107/in+docs
Del: other reasons-- https://master.php.net/note/delete/95107
Reject -- https://master.php.net/note/reject/95107
Search -- https://master.php.net/manage/user-notes.php