戏说PHP的嵌套函数

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

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

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

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

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

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

}

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

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

Php代码  收藏代码
  1. <?php  
  2. function foo()  
  3. {  
  4.   function bar()  
  5.   {  
  6.     echo "I don't exist until foo() is called. ";  
  7.   }  
  8. }  
  9.   
  10. /* 现在还不能调用bar()函数,因为它还不存在 */  
  11.   
  12. foo();  
  13.   
  14. /* 现在可以调用bar()函数了,因为foo()函数 
  15.    的执行使得bar()函数变为已定义的函数 */  
  16.   
  17. bar();  
  18.   
  19. ?>   
<?php
function foo()
{
  function bar()
  {
    echo "I don't exist until foo() is called.
";
  }
}

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

foo();

/* 现在可以调用bar()函数了,因为foo()函数
   的执行使得bar()函数变为已定义的函数 */

bar();

?> 

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

我们不妨先看一下函数:

Php代码  收藏代码
  1. function outer( $msg ) {   
  2.     function inner( $msg ) {   
  3.         echo 'inner: '.$msg.' ';   
  4.     }   
  5.     echo 'outer: '.$msg.' ';   
  6.     inner( $msg );   
  7. }   
  8.    
  9. inner( 'test1' );  // Fatal error:  Call to undefined function inner()   
  10. //上面出错,是因为外部函数还没有调用,所以出错。  
  11. outer( 'test2' );  // outer: test2 inner: test2   
  12. inner( 'test3' );  // inner: test3   
  13. outer( 'test4' );  // Fatal error:  Cannot redeclare inner()   
  14. //上面出错,是因为,外部函数被调用时,内部函数被重定义了。  
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代码  收藏代码
  1. static public function initAutoload(){  
  2.         //初始化Autoload Callable List  
  3.         self::setAutoloadCallableList();  
  4.         //初始化 $classList  
  5.         self::$classList = uxAutoloadConfig::getClassList();  
  6.   
  7.         //如果有spl_autoload_register,则直接设置  
  8.         if (function_exists('spl_autoload_register')){  
  9.             ini_set('unserialize_callback_func''spl_autoload_call');  
  10.             spl_autoload_register(array('uxAutoload''splSimpleAutoload'));  
  11.         }elseif (!function_exists('__autoload')){  //否则要使用__autoload函数。  
  12.             ini_set('unserialize_callback_func''__autoload');  
  13.   
  14.             //因为没有spl_autoload, 所以, 这里要定义一个__autoload函数.  
  15.             function __autoload($class){  
  16.                 if( self::splSimpleAutoload($class)== true)  
  17.                     return true;  
  18.                 //因为没有spl_autoload_register,所以在类未加载成功时,要处理Callable List  
  19.                 foreach(self::$autoloadCallables as $key => $callable ){  
  20.                     if (class_exists($class, false)){  
  21.                         $classObj=self::$autoloadObjectList[$callable[0]];  
  22.                     }else{  
  23.                         $className=$callable[0];  
  24.                         $classObj = new $className();  
  25.                         self::$autoloadObjectList[$class] = &$classObj;  
  26.                     }  
  27.                     if (method_exists($classObj,$callable[1])){  
  28.                         $method=$callable[1];  
  29.                         if ($classObj->$method($class)==true)  
  30.                             return true;  
  31.                     }else{  
  32.                         trigger_error('Autoload method '.$method.' not found in class '.$className.'!', E_USER_ERROR);  
  33.                         return false;  
  34.                     }  
  35.                 }  
  36.             }  
  37.         }  
  38.     }  
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代码  收藏代码
  1.   static public function getInstance($config = 0 ,$className=NULL){  
  2.       if (!function_exists('trace')){ //specially for ajax debug!!  
  3.           function trace($var){  
  4.               $string=print_r($var,true);  
  5.               require_once(UXERHDIR.'../uxLogger/uxLogger.class.php');  
  6.               uxLogger::getInstance()->logg('INFO',  
  7.               '/*************************** BEGIN INFO BY TRACE: ***************************  '  
  8.               .$string   
  9.               .'/***************************  END INFO BY TRACE   *************************** ' );  
  10.           }  
  11.       }  
  12.       if (!isset(self::$instance)){  
  13.           if (is_array($config)){  
  14.               $options=$config;  
  15.           }else{    
  16.                   if ($config == NULL)  
  17. $config = 0;  
  18.               $options=uxErrorHandlerConfig::get($config);  
  19.           }  
  20.           $class=($className==NULL)?'uxErrorHandler':$className;  
  21.           self::$instance = new $class($options);  
  22.       }  
  23.       return self::$instance;  
  24.   }  
    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: ***************************
 '
                .$string 
                .'/***************************  END INFO BY TRACE   ***************************
' );
            }
        }
        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;
    }

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

原文地址:https://www.cnblogs.com/walter371/p/4535016.html