Home > Backend Development > PHP Tutorial > 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?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-03-02 12:44:01
Original
1281 people have browsed it

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

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

Return array.

But

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

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

Return array.

But

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

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
Composer cannot install laravel
From 1970-01-01 08:00:00
0
0
0
Laravel Space/laravel-backup cannot be installed
From 1970-01-01 08:00:00
0
0
0
Laravel 5.1 Login laravel comes with it No more
From 1970-01-01 08:00:00
0
0
0
Why thinkphp has better performance than laravel?
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template