Home > Article > Backend Development > The application object $app in laravel is written as $app[$k] but no error is reported! Why?
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
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
In short, $app is obviously an object, how can it be written in the form $app[$k]?
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
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
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