失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 为什么戏说php 戏说PHP的嵌套函数

为什么戏说php 戏说PHP的嵌套函数

时间:2020-11-18 03:16:26

相关推荐

为什么戏说php 戏说PHP的嵌套函数

PHP很早就支持嵌套函数了。并是不PHP5.3有闭包时才有的。然而,它却不是象JS,AS那样的闭包嵌套。即它的嵌套函数根本无闭包模式的逃脱。

PHP嵌套函数有一些特别之处。最特别的是,当外部函数被调用时,内部函数就会自动进入全局域中,成为新的定义函数。

所以,当外部函数确保是被调用一次,不会被调用二次,那么,可以写嵌套函数在其中。否则,就会引发致命错误。

但若我们仍想在一个可被调用多次的函数中定义一个内部函数,那么,该如何处理?

我们象在全局定义函数一样使用:

if (!function_exists(‘你的函数名‘)){

}

因此,全局函数的使用,常常用于一些特别的目的。同时要清楚,这样的函数,实际就是定义的全局函数。因此,它没有类对它封装,更没有命名空间。

看一下PHP手册中是如何说的:

Php代码

functionfoo()

{

functionbar()

{

echo"Idon‘texistuntilfoo()iscalled.\n";

}

}

/*现在还不能调用bar()函数,因为它还不存在*/

foo();

/*现在可以调用bar()函数了,因为foo()函数

的执行使得bar()函数变为已定义的函数*/

bar();

?>

function foo()

{

function bar()

{

echo "I don‘t exist until foo() is called.\n";

}

}

/* 现在还不能调用bar()函数,因为它还不存在 */

foo();

/* 现在可以调用bar()函数了,因为foo()函数

的执行使得bar()函数变为已定义的函数 */

bar();

?>

PHP 中的所有函数和类都具有全局作用域,可以在内部定义外部调用,反之亦然。

我们不妨先看一下函数:

Php代码

functionouter($msg){

functioninner($msg){

echo‘inner:‘.$msg.‘‘;

}

echo‘outer:‘.$msg.‘‘;

inner($msg);

}

inner(‘test1‘);//Fatalerror:Calltoundefinedfunctioninner()

//上面出错,是因为外部函数还没有调用,所以出错。

outer(‘test2‘);//outer:test2inner:test2

inner(‘test3‘);//inner:test3

outer(‘test4‘);//Fatalerror:Cannotredeclareinner()

//上面出错,是因为,外部函数被调用时,内部函数被重定义了。

function outer( $msg ) {

function inner( $msg ) {

echo ‘inner: ‘.$msg.‘ ‘;

}

echo ‘outer: ‘.$msg.‘ ‘;

inner( $msg );

}

inner( ‘test1‘ ); // Fatal error: Call to undefined function inner()

//上面出错,是因为外部函数还没有调用,所以出错。

outer( ‘test2‘ ); // outer: test2 inner: test2

inner( ‘test3‘ ); // inner: test3

outer( ‘test4‘ ); // Fatal error: Cannot redeclare inner()

//上面出错,是因为,外部函数被调用时,内部函数被重定义了。

这里,我们再看一下,一个自动加载类,其中的做法

Php代码

staticpublicfunctioninitAutoload(){

//初始化AutoloadCallableList

self::setAutoloadCallableList();

//初始化$classList

self::$classList=uxAutoloadConfig::getClassList();

//如果有spl_autoload_register,则直接设置

if(function_exists(‘spl_autoload_register‘)){

ini_set(‘unserialize_callback_func‘,‘spl_autoload_call‘);

spl_autoload_register(array(‘uxAutoload‘,‘splSimpleAutoload‘));

}elseif(!function_exists(‘__autoload‘)){//否则要使用__autoload函数。

ini_set(‘unserialize_callback_func‘,‘__autoload‘);

//因为没有spl_autoload,所以,这里要定义一个__autoload函数.

function__autoload($class){

if(self::splSimpleAutoload($class)==true)

returntrue;

//因为没有spl_autoload_register,所以在类未加载成功时,要处理CallableList

foreach(self::$autoloadCallablesas$key=>$callable){

if(class_exists($class,false)){

$classObj=self::$autoloadObjectList[$callable[0]];

}else{

$className=$callable[0];

$classObj=new$className();

self::$autoloadObjectList[$class]=&$classObj;

}

if(method_exists($classObj,$callable[1])){

$method=$callable[1];

if($classObj->$method($class)==true)

returntrue;

}else{

trigger_error(‘Autoloadmethod‘.$method.‘notfoundinclass‘.$className.‘!‘,E_USER_ERROR);

returnfalse;

}

}

}

}

}

static public function initAutoload(){

//初始化Autoload Callable List

self::setAutoloadCallableList();

//初始化 $classList

self::$classList = uxAutoloadConfig::getClassList();

//如果有spl_autoload_register,则直接设置

if (function_exists(‘spl_autoload_register‘)){

ini_set(‘unserialize_callback_func‘, ‘spl_autoload_call‘);

spl_autoload_register(array(‘uxAutoload‘, ‘splSimpleAutoload‘));

}elseif (!function_exists(‘__autoload‘)){ //否则要使用__autoload函数。

ini_set(‘unserialize_callback_func‘, ‘__autoload‘);

//因为没有spl_autoload, 所以, 这里要定义一个__autoload函数.

function __autoload($class){

if( self::splSimpleAutoload($class)== true)

return true;

//因为没有spl_autoload_register,所以在类未加载成功时,要处理Callable List

foreach(self::$autoloadCallables as $key => $callable ){

if (class_exists($class, false)){

$classObj=self::$autoloadObjectList[$callable[0]];

}else{

$className=$callable[0];

$classObj = new $className();

self::$autoloadObjectList[$class] = &$classObj;

}

if (method_exists($classObj,$callable[1])){

$method=$callable[1];

if ($classObj->$method($class)==true)

return true;

}else{

trigger_error(‘Autoload method ‘.$method.‘ not found in class ‘.$className.‘!‘, E_USER_ERROR);

return false;

}

}

}

}

}

很明显,它是定义了一个内部函数function __autoload($class),以防没有‘spl_autoload_register‘。而这个类的这个函数,任一request请求中,只运行一次。

但是,如果要运行多次,比如,以下函数中,定义了一个全局的TRACE函数。这个函数的目的是在用户使用标准MVC方式时,才提供此TRACE函数给用户使用。引导用户走正确的方向。实际上,也可以看出,如果用户用不到此类,很可能,TRACE函数就不是这么几行代码。由此,这一做法确实精简了相当多的代码。

Php代码

staticpublicfunctiongetInstance($config=0,$className=NULL){

if(!function_exists(‘trace‘)){//speciallyforajaxdebug!!

functiontrace($var){

$string=print_r($var,true);

require_once(UXERHDIR.‘../uxLogger/uxLogger.class.php‘);

uxLogger::getInstance()->logg(‘INFO‘,

‘/***************************BEGININFOBYTRACE:***************************\r\n‘

.$string

.‘/***************************ENDINFOBYTRACE***************************\r\n‘);

}

}

if(!isset(self::$instance)){

if(is_array($config)){

$options=$config;

}else{

if($config==NULL)

$config=0;

$options=uxErrorHandlerConfig::get($config);

}

$class=($className==NULL)?‘uxErrorHandler‘:$className;

self::$instance=new$class($options);

}

returnself::$instance;

}

static public function getInstance($config = 0 ,$className=NULL){

if (!function_exists(‘trace‘)){ //specially for ajax debug!!

function trace($var){

$string=print_r($var,true);

require_once(UXERHDIR.‘../uxLogger/uxLogger.class.php‘);

uxLogger::getInstance()->logg(‘INFO‘,

‘/*************************** BEGIN INFO BY TRACE: ***************************\r\n ‘

.$string

.‘/*************************** END INFO BY TRACE ***************************\r\n‘ );

}

}

if (!isset(self::$instance)){

if (is_array($config)){

$options=$config;

}else{

if ($config == NULL)

$config = 0;

$options=uxErrorHandlerConfig::get($config);

}

$class=($className==NULL)?‘uxErrorHandler‘:$className;

self::$instance = new $class($options);

}

return self::$instance;

}

可以看出,嵌套函数,是一种有条件全局函数,你可以控制,在什么情况下提供这样的全局函数给用户使用。但也需要注意,过多的全局函数则会产生“全局污染”,所以,不可多用。

如果觉得《为什么戏说php 戏说PHP的嵌套函数》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。