views:

185

answers:

4

I would like to use my function, for example DebugR(), but I don't want to use a require or include (with include_path) to load the function file that contains the source.

I know I could use an autoload, but this action must be generic in my php configuration. I think I must create a PHP extension, but is there another way?

A: 

Without building the extension, or auto-loading a class of functions or directly requiring the function into the page, or writing the function in the same file, I'm pretty sure there isn't a way without adjusting your PHP configuration.

Out of curiosity, what are the reasons for not wanting to do any of those things?

jerebear
A: 

Why not use an accelerator which caches the code as BC in the memory?
You still need to write the "include" directive.
If extension is where you head, then fork from any of the available open source Accelerators out there.

Itay Moav
+5  A: 

There is a PHP configuration line you can do.

The documentation says this :

auto_prepend_file string

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.
Erick
A: 

The only feasible way to do that is to use autoloads like this:

// 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';
    }
}

Of course, every function would be stored inside this _ class.

Another alternative, if you are working from within an object is to use the __call() magic method and do something like:

return $this->DebugR();
"I know I could use an autoload, but this action must be generic"I don't want to use autoload. Read the voted answer, it's the best response of the problem.
Kevin Campion