What's __halt_compiler in PHP for?

前端 未结 3 1717
谎友^
谎友^ 2020-12-23 17:19

From the manual:

void __halt_compiler ( void )

This function halts the execution of the compiler. This can be useful to embe

3条回答
  •  攒了一身酷
    2020-12-23 17:52

    Previously, The ClassGenerator in the PhpSpec unit testing library provided a good example of using __halt_compiler(), which the PHP class contains a code template for a PHP class.

    They've recently update to read the template from a seperate file, but initially the getTemplate() method will attempt to read the PHP code template provided in the file that follows the __halt_compiler() call. This avoids the token from getting parsed.

    /**
     * The Class Generator is responsible for generating the classes from a resource
     * in the appropriate folder using the template provided
     */
    class ClassGenerator
    {
        //...
    
        /**
         * @return string
         */
        protected function getTemplate()
        {
            return file_get_contents(__FILE__, null, null, __COMPILER_HALT_OFFSET__);
        }
    }
    __halt_compiler();

提交回复
热议问题