note 113301 deleted from function.include by peehaa
| From: | peehaa@php.net | Date: | Fri, 09 Dec 2016 22:55:32 +0000 |
| Subject: | note 113301 deleted from function.include by peehaa | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208400@lists.php.net to get a copy of this message | ||
Note Submitter: casey at seangroup dot com
----
Include function,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).
<?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);
}
?>
-------------------------------------------
www.show-ip.org