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

Source: Internet
Author: User
{Code...} is used in the getConfig method of IlluminateFilesystemFilesystemManager in laravel5.1 to return an array. But {code...} is clearly an object. Can an object use the key value of an array? This is obviously wrong in syntax, but it's amazing...

InLaravel 5.1In
IlluminateFilesystemFilesystemManagerClass
GetConfigMethod
Actually used

$this->app['config']["filesystems.disks.{$name}"]);

Returns an array.

However

$this->app

It is clearly an object.
Can an object use the key value of an array? This is obviously wrong in syntax, but the magic thing is still happening.

This is the getConfig method.

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

I separate dd ($ this-> app );
That is

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

Output

However, I dd ($ this-> app 'config'); that is

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

The output is as follows:

In short, $ app is clearly an object. how can it be written as $ app [$ k?

Reply content:

InLaravel 5.1In
IlluminateFilesystemFilesystemManagerClass
GetConfigMethod
Actually used

$this->app['config']["filesystems.disks.{$name}"]);

Returns an array.

However

$this->app

It is clearly an object.
Can an object use the key value of an array? This is obviously wrong in syntax, but the magic thing is still happening.

This is the getConfig method.

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

I separate dd ($ this-> app );
That is

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

Output

However, I dd ($ this-> app 'config'); that is

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

The output is as follows:

In short, $ app is clearly an object. how can it be written as $ app [$ k?

App inherited fromIlluminateContainerContainerWhile the Container implementsArrayAccess(Http://php.net/manual/zh/clas...) interface. The ArrayAccess interface provides the ability to access objects just like an array. you can call the isset, unset, and [] methods to access values as long as you implement several methods of the interface.

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

ArrayAccess

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.