名前空間でのPHPオートローディング
-
30-09-2019 - |
質問
私は自分の名前空間でオートローディングにわずかな問題を抱えています。ここのPHPマニュアルに示されているように: http://us.php.net/manual/en/language.namespaces.rules.php 完全に適格な名前eg glue common is_email()を使用して、名前空間機能を自動装置できるはずです。
問題は、私が関数spl_autoload_register(array($ import、 "load"))を持っているということです。初期の名前空間内では、最初の名前空間から glue common is_email()を呼び出しようとするたびに、そのautoload関数は渡されませんが、new is_email()を使用すると(クラスのコンテキストで)。マニュアルは、完全に資格のある名前から自動装着できると言っていますが、できません。
これが私のコードです:
namespace glue;
require_once 'import.php';
use glue\import as import;
use glue\core\router as router;
$import = new import();
spl_autoload_register(array($import, "load"));
/** Works and echos glue\router **/
$router = new router();
/** Don't do nothing **/
$cheese = \glue\common\is_email($email);
このコードも試しました。
namespace glue;
require_once 'import.php';
use glue\import as import;
use glue\core\router as router;
use glue\common;
$import = new import();
spl_autoload_register(array($import, "load"));
/** Works and echos glue\router **/
$router = new router();
/** Don't do nothing **/
$cheese = common\is_email($email);
そして最後にこのコード:
namespace glue;
require_once 'import.php';
use glue\import as import;
use glue\core\router as router;
use glue\common\is_email as F;
$import = new import();
spl_autoload_register(array($import, "load"));
/** Works and echos glue\router **/
$router = new router();
/** Don't do nothing **/
$cheese = F($email);
解決
これが唯一の正しい答えです。
すべての名前空間には、独自のspl_autoload_register()関数が必要です。
また、 spl_autoload_register()syntax かわった 5.3:
spl_autoload_register(__NAMESPACE__ . "\\className::functionName"));
以下は機能するはずです。
namespace glue;
require_once 'import.php';
use glue\import as import;
use glue\core\router as router;
$import = new import();
spl_autoload_register(__NAMESPACE__ . "\\$import::load"));
/** Works and echos glue\router **/
$router = new router();
/** Don't do nothing **/
$cheese = \glue\common\is_email($email);
これがいくつかあります 住む 機能するコード!
in ../webpageconsolidator.inc.php:
class WebPageConsolidator
{
public function __construct() { echo "PHP 5.2 constructor.\n"; }
}
test.php:
<?php
namespace WebPage;
class MyAutoloader
{
public static function load($className)
{
require '../' . __NAMESPACE__ . $className . '.inc.php';
}
}
spl_autoload_register(__NAMESPACE__ . "\\MyAutoloader::load");
class Consolidator extends \WebpageConsolidator
{
public function __construct()
{
echo "PHP 5.3 constructor.\n";
parent::__construct();
}
}
// Output:
// PHP 5.3 constructor.
// PHP 5.2 constructor.
だから私はそれがうまくいくことを知っています。
他のヒント
使用する 作曲 PHPクラスをオートロードします。
私の最近のブログ投稿でそれを行う方法をチェックしてください: https://enchanterio.github.io/enterprise-level-php/2017/12/25/the-magic-behind-autoloading-php-files-using-composer.html
OPの問題の誤解は、おそらく機能/方法が自動装置の対象となることです。オートローディングは、クラスを参照することによってのみトリガーされます。
これはまだ残っていると言われています 名前空間での自動装置クラスについての質問:
2017年の時点で現在 php-fig オートローディングの標準は、名前のペース化されたクラスに次のオートローダーコードを提供するPSR-4です。
<?php
/**
* An example of a project-specific implementation.
*
* After registering this autoload function with SPL, the following line
* would cause the function to attempt to load the \Foo\Bar\Baz\Qux class
* from /path/to/project/src/Baz/Qux.php:
*
* new \Foo\Bar\Baz\Qux;
*
* @param string $class The fully-qualified class name.
* @return void
*/
spl_autoload_register(function ($class) {
// project-specific namespace prefix
$prefix = 'Foo\\Bar\\';
// base directory for the namespace prefix
$base_dir = __DIR__ . '/src/';
// does the class use the namespace prefix?
$len = strlen($prefix);
if (strncmp($prefix, $class, $len) !== 0) {
// no, move to the next registered autoloader
return;
}
// get the relative class name
$relative_class = substr($class, $len);
// replace the namespace prefix with the base directory, replace namespace
// separators with directory separators in the relative class name, append
// with .php
$file = $base_dir . str_replace('\\', '/', $relative_class) . '.php';
// if the file exists, require it
if (file_exists($file)) {
require $file;
}
});
完全な仕様テキストはで見つけることができます PSR-4:オートローダー.
上記のコード例(および別のコードの例 多数 名前空間)はで見つけることができます PSR-4の実装の例 (またはgithub: イチジクスタンダード/受け入れ/psr-4-autoloader-examples.md).