التحميل التلقائي PHP في مساحات الأسماء
-
30-09-2019 - |
سؤال
لقد واجهت مشكلة بسيطة مع التحميل التلقائي في مساحة اسمي. كما هو موضح في دليل PHP هنا: http://us.php.net/manual/en/language.namespaces.rules.php يجب أن تكون قادرًا على تشغيل وظائف مساحة الاسم ذات الأوتوماتيكية ذات الاسم الكامل المؤهل على سبيل المثال glue common is_email ().
الشيء هو أن لدي وظيفة spl_autoload_register (Array ($ import ، "load")) ؛ ضمن مساحة الاسم الأولية ولكن كلما حاولت الاتصال glue common is_email () من مساحة الاسم الأولية ، فلن يمر هذه الوظيفة التلقائية ولكن عند استخدام 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 () بناء الجملة تغير في 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);
هنا بعض يعيش رمز يعمل فقط!
في ../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: Autoloader.
مثال الرمز أعلاه (وواحد آخر للتحميل التلقائي من مضاعف يمكن العثور على مساحات الأسماء) في مثال على تطبيقات PSR-4 (أو جيثب: معايير التين/مقبولة/PSR-4-Autoloader-examples.md).