简体   繁体   English

laravel如何在Facade类中使用object作为数组

[英]How laravel uses object as an array in Facade class

I've noted that for creating a facade class, laravel provides only name "db" 我注意到,为了创建一个Facade类,laravel只提供名称“db”

framework/src/Illuminate/Support/Facades/DB.php 框架/ src目录/照亮/支持/幕墙/ db.php中

class DB extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'db';
    }
}

I looked deeper and figured out that this method uses the provided name 我看得更深,并发现这个方法使用提供的名称

framework/src/Illuminate/Support/Facades/Facade.php 框架/ src目录/照亮/支持/幕墙/ Facade.php

protected static function resolveFacadeInstance($name)
{
    if (is_object($name)) {
        return $name;
    }
    if (isset(static::$resolvedInstance[$name])) {
        return static::$resolvedInstance[$name];
    }
    return static::$resolvedInstance[$name] = static::$app[$name];
}

I understand first and second If statements. 我理解第一和第二个If语句。

But I have problems with understanding this: 但我理解这个问题:

return static::$resolvedInstance[$name] = static::$app[$name]

As I understood that $app is a protected property of Facade class which contains an instance of \\Illuminate\\Contracts\\Foundation\\Application class. 据我所知, $appFacade类的受保护属性,它包含\\Illuminate\\Contracts\\Foundation\\Application类的实例。

/**
     * The application instance being facaded.
     *
     * @var \Illuminate\Contracts\Foundation\Application
     */
    protected static $app;

My two questions: 我的两个问题:

How is it possible to use an object as an array( static::$app[$name] ) if Application class doesn't extends ArrayObject class? 如果Application类没有扩展ArrayObject类,如何将对象用作数组( static::$app[$name] )?

How laravel understands which class to call with providing only a short name 'db'? laravel如何通过仅提供短名称“db”来了解要调用哪个类?

Clicking through the Laravel source, I found this. 单击Laravel源代码,我发现了这一点。 As you can see, ApplicationContract (the private static $app from your question) is implemented by Application . 如您所见, ApplicationContract (来自您的问题的private static $app )由Application实现。 This is in turn derived from Container , which implements the PHP core ArrayAccess interface. 这又是从Container派生的,它实现了PHP核心ArrayAccess接口。 Carefully implementing this whole chain eventually makes Applicatin accessible like an array. 仔细地实现这整个链最终使Applicatin像数组一样可访问。

Turns out it boils down to good ole' object oriented programming :) 事实证明它归结为良好的'面向对象编程:)

// Illuminate/Foundation/Application.php
class Application extends Container implements ApplicationContract, HttpKernelInterface
                          ^^^^^^^^^            ^-> the private static $app in your question.

// Illuminate/Container/Container.php
class Container implements ArrayAccess, ContainerContract
                           ^^^^^^^^^^^

// PHP core ArrayAccess documentation
/**
 * Interface to provide accessing objects as arrays.
 * @link http://php.net/manual/en/class.arrayaccess.php
 */
interface ArrayAccess {

You can look this, php manual and use ArrayAccess interface: 你可以看看这个,php手册并使用ArrayAccess接口:

http://php.net/manual/en/class.arrayaccess.php http://php.net/manual/en/class.arrayaccess.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM