【问题标题】:How to use php function without load source file?如何在不加载源文件的情况下使用php函数?
【发布时间】:2010-10-18 15:24:23
【问题描述】:

我想使用我的函数,例如 DebugR(),但我不想使用 require 或 include(使用 include_path)来加载包含源代码的函数文件。

我知道我可以使用自动加载,但是这个操作在我的 php 配置中必须是通用的。我想我必须创建一个 PHP 扩展,但是还有其他方法吗?

【问题讨论】:

  • 谢谢 Jim Puls,我会记住如何编辑未来问题

标签: php function


【解决方案1】:

有一个 PHP 配置行你可以做。

文档是这样说的:

auto_prepend_file 字符串

Specifies the name of a file that is automatically parsed before the main file. The file is included as if it was called with the require() function, so include_path is used.

The special value none disables auto-prepending.

【讨论】:

    【解决方案2】:

    如果不构建扩展,或者自动加载一类函数,或者直接将函数请求到页面中,或者在同一个文件中编写函数,我很确定没有办法不调整你的 PHP 配置.

    出于好奇,不想做这些事情的原因是什么?

    【讨论】:

      【解决方案3】:

      为什么不使用将代码作为 BC 缓存在内存中的加速器?
      您仍然需要编写“包含”指令。
      如果扩展是你要去的地方,那么从任何可用的开源加速器那里分叉。

      【讨论】:

        【解决方案4】:

        唯一可行的方法是使用这样的自动加载:

        // your_file.php
        function __autoload($class)
        {
            require_once('./classes/' . $class . '.php');
        }
        
        echo _::Bar();
        var_dump(_::DebugR());
        echo _::Foo();
        
        // ./classes/_.php
        class _
        {
            function Bar()
            {
                return 'bar';
            }
        
            function DebugR()
            {
                return true;
            }
        
            function Foo()
            {
                return 'foo';
            }
        }
        

        当然,每个函数都会存储在这个 _ 类中。

        如果您在对象内工作,另一种选择是使用 __call() 魔术方法并执行以下操作:

        return $this->DebugR();
        

        【讨论】:

        • “我知道我可以使用自动加载,但这个操作必须是通用的”我不想使用自动加载。阅读投票答案,这是对问题的最佳回应。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-06-03
        • 1970-01-01
        • 2023-03-04
        • 1970-01-01
        • 1970-01-01
        • 2015-06-08
        • 2021-03-15
        相关资源
        最近更新 更多