PHP嵌套类工作......有点?

Sea*_*nJA 8 php nested-class

所以,如果你尝试做这样的嵌套类:

//nestedtest.php

class nestedTest{
    function test(){
         class E extends Exception{}
         throw new E;
    }
}
Run Code Online (Sandbox Code Playgroud)

你会收到一个错误 Fatal error: Class declarations may not be nested in [...]

但如果你在一个单独的文件中有一个类,如下所示:

//nestedtest2.php

class nestedTest2{
    function test(){
         include('e.php');
         throw new E;
    }
}

//e.php
class E Extends Exception{}
Run Code Online (Sandbox Code Playgroud)

那么,为什么第二种hacky方式可以正常工作,但这种非hacky方式不起作用?

Tom*_*igh 18

从手册(http://php.net/manual/en/function.include.php):

包含文件时,它包含的代码将继承发生包含的行的变量范围.从那时起,调用文件中该行可用的任何变量都将在被调用文件中可用.但是,包含文件中定义的所有函数和类都具有全局范围.