Rather than just writing a new function called import() i'd like to know if there's a better solution. Otherwise require_once would be included in the scope of import() only, which is bad for any "global开发者_如何学编程" variable there.
My import() function would work differently than require_once, but serves the same purpose (enhanced usability).
My gut instict opinion: Nah. Don't do it.
A language has its native set of functions. Why introduce a proprietary function that has no added value.
I don't think there is reason to be scared off by
require_once()
.I agree with you, using globals is not a great thing, but sacrificing the possibility of using them for a vanity function name is not a good way to go.
If paths are what you're worried about, then why not write an import()
function that returns the correct path. Like so:
require_once import("operations.php");
the only - in my eyes not so horrible - downside is that you only have to make sure that import always returns a correct path to something, otherwise require_once
will crash. Other than that, it has the same comfort, allows you to control paths as you wish, but doesn't cost you anything in terms of flexibility.
I don't know about you and your team but I've never met a PHP developer "scared off" by require/include - that's just the way the language works.
If you're scared off by such things perhaps you should be using another language.
And what you're proposing sounds like a maintainence nightmare.
You could possibly get something similar to what you're after by using __autoload()
. Then you could just use whatever class you want, without require
ing it, and so long as its file is in your include_path it will be loaded without trouble.
(This of course assumes that the file you're trying to include has a class in it, and that the class and its file are named according to some convention. Probably that's not actually the case.)
This is a pretty standard autoloader, that works with Zend- and Pear-style naming conventions:
<?php
/**
* Load (include the file of) a given PHP class.
*
* @param string $class The name of the class to load.
*
* @return void
*/
function __autoload($class)
{
$files = array(
$class . '.php',
str_replace('_', '/', $class) . '.php',
);
foreach (explode(PATH_SEPARATOR, ini_get('include_path')) as $base_path) {
foreach ($files as $file) {
$path = "$base_path/$file";
if (file_exists($path) && is_readable($path)) {
include_once $path;
$finalPath = $path;
}
}
}
if (isset($finalPath)) {
return;
} else {
die("Unable to load class '$class'.");
}
}
?>
You could cut down on the number of require
calls by creating an import function and attaching it to a class. So after requiring the framework once, users could then use the import function for including other framework classes. To wit:
index.php:
require_once 'MyFramework.php';
MyFramework::import('Path/To/File.php');
MyFramework.php
class MyFramework {
static function import($path) {
// code to include/import the path
}
}
Another option would be to use the PHP's auto_load semantics to automatically require
framework classes:
http://php.net/manual/en/language.oop5.autoload.php
Though at the end of the day you can't really replace the require/include statements, short of editing the PHP source.
精彩评论