PHP使用包含内部函数

前端之家收集整理的这篇文章主要介绍了PHP使用包含内部函数前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图创建一个我可以调用函数,如下所示,
view( 'archive','post.PHP' );

而这个功能真正起作用的是什么.

include( 'view/archive/post.PHP' );

原因是如果将来我将目录扩展为view / archive / another_level / post.PHP我不想在我的代码中回到任何地方并更改所有包含路径.

目前这是我的功能,除了看起来include正在函数内部调用,而不是在调用函数时被调用

function view( $view,$file )
    {
        switch ( $view )
        {
            case 'archive'  : $view = 'archive/temp';   break;
            case 'single'   : $view = 'single';     break;
        }

        include( TEMPLATEPATH . "/view/{$view}/{$file}" );
    }

如何才能使此功能正确包含该文件

编辑:

没有显示错误.感谢@Ramesh的错误检查代码,ini_set(‘display_errors’,’On’)我能够看到包含文件中还有其他“未显示错误,这似乎导致文件无法显示起来……

该用例是 explicitly documented

If the include occurs inside a function within the calling file,then
all of the code contained in the called file will behave as though it
had been defined inside that function. So,it will follow the variable
scope of that function. An exception to this rule are magic constants
which are evaluated by the parser before the include occurs.

恕我直言,在常量中保持基本路径更简单(你似乎已经在某种程度上做了)或甚至进行全站点搜索和替换(在任何体面的编辑器中这是一个30秒的任务),而不是重写所有包含的文件使用全局变量.

原文链接:https://www.f2er.com/php/134632.html

猜你在找的PHP相关文章