Home  >  Article  >  Backend Development  >  The application object $app in laravel is written as $app[$k] but no error is reported! Why?

The application object $app in laravel is written as $app[$k] but no error is reported! Why?

WBOY
WBOYOriginal
2016-12-01 01:27:431218browse

In the getConfig
method in the IlluminateFilesystemFilesystemManager
class in laravel 5.1,
actually uses

<code class="php">$this->app['config']["filesystems.disks.{$name}"]);</code>

Return array.

But

<code class="php">$this->app</code>

Obviously a target.
Can an object be retrieved using the key value of an array? This is obviously grammatically wrong but something magical happened anyway

This is the getConfig method

<code class="php"> /**
     * Get the filesystem connection configuration.
     *
     * @param  string  $name
     * @return array
     */
    protected function getConfig($name)
    {
       
        return $this->app['config']["filesystems.disks.{$name}"];
    }</code>

I alone dd($this->app);
That is as follows

<code class="php"> /**
     * Get the filesystem connection configuration.
     *
     * @param  string  $name
     * @return array
     */
    protected function getConfig($name)
    {
        dd($this->app);
        return $this->app['config']["filesystems.disks.{$name}"];
    }</code>

Output

The application object $app in laravel is written as $app[$k] but no error is reported! Why?

But I dd($this->app'config'); which is

<code class="php"> protected function getConfig($name)
    {
        dd($this->app['config']["filesystems.disks.{$name}"]);
        return $this->app['config']["filesystems.disks.{$name}"];
    }</code>

Then the output is as follows

The application object $app in laravel is written as $app[$k] but no error is reported! Why?

In short, $app is obviously an object, how can it be written in the form $app[$k]?

Reply content:

In the getConfig
method in the IlluminateFilesystemFilesystemManager
class in laravel 5.1,
actually uses

<code class="php">$this->app['config']["filesystems.disks.{$name}"]);</code>

Return array.

But

<code class="php">$this->app</code>

Obviously a target.
Can an object be retrieved using the key value of an array? This is obviously grammatically wrong but something magical happened anyway

This is the getConfig method

<code class="php"> /**
     * Get the filesystem connection configuration.
     *
     * @param  string  $name
     * @return array
     */
    protected function getConfig($name)
    {
       
        return $this->app['config']["filesystems.disks.{$name}"];
    }</code>

I alone dd($this->app);
That is as follows

<code class="php"> /**
     * Get the filesystem connection configuration.
     *
     * @param  string  $name
     * @return array
     */
    protected function getConfig($name)
    {
        dd($this->app);
        return $this->app['config']["filesystems.disks.{$name}"];
    }</code>

Output

The application object $app in laravel is written as $app[$k] but no error is reported! Why?

But I dd($this->app'config'); which is

<code class="php"> protected function getConfig($name)
    {
        dd($this->app['config']["filesystems.disks.{$name}"]);
        return $this->app['config']["filesystems.disks.{$name}"];
    }</code>

Then the output is as follows

The application object $app in laravel is written as $app[$k] but no error is reported! Why?

In short, $app is obviously an object, how can it be written in the form $app[$k]?

app inherits from IlluminateContainerContainer, and Container implements the ArrayAccess (http://php.net/manual/zh/clas...) interface. The ArrayAccess interface provides the ability to access objects just like accessing arrays. As long as you implement a few methods of the interface, you can call isset, unset, [] to access values.

$this->app['config'] is also an object IlluminateConfigRepository It also implements ArrayAccess, so it can also be used as an array.

ArrayAccess

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn