1

If I write this line on routes/web.php file

dd( app()['config']["auth.guards.web"] );

It outputs this:

array:2 [▼
  "driver" => "session"
  "provider" => "users"
]

That's cool, but my question is since app()['config'] returns an object so how this ["auth.guards.web"]works? Even there is no index with that name!

Outside of Laravel I tried to write a class named Test so that it returns the same output but I got an error! Which is,

Fatal error: Uncaught Error: Cannot use object of type Test as array 

Can anyone explain it with core php?

1
  • 1
    The object returned by app()['config'] probably implements ArrayAccess Commented Oct 19, 2020 at 1:46

1 Answer 1

3

Laravel contains a very useful helper function called Arr::get() which retrieves a value from a deeply nested array using "dot" notation. Laravel framework uses this helper function everywhere it needs to get the values inside a deep array; e.g configs and translation and so on.

Example;

config('database.driver');
__('validation.error');
trans('validation.error');

On the other hand, app()['config'] returns Illuminate\Config\Repository instance which implements ArrayAccess. As long as get method of Illuminate\Config\Repository class uses Arr::get method, you can use "dot" notated array access along with app()['config'].

For more details, see these links; https://laravel.com/docs/8.x/helpers#method-array-get https://github.com/laravel/framework/blob/8.x/src/Illuminate/Config/Repository.php#L53

Sign up to request clarification or add additional context in comments.

1 Comment

I got it, ArrayAccess is a php's predefined interface and it provides accessing objects as an array. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.