2

Okay there are questions about the same topic before but they don't help to fully understand this topic

SO SuggestionFirst
SO Suggestion Second

All the code is just to illustrate the situation, So this is the structure

A helper function which does something

namespace App\Helpers;

class Pets{

    public function limit($string,$limit,$start = 0){
        return substr($string,$start,$limit);
    }
}

Now in order to use this helper, since it's a class so i need to create an object like this


CODE SAMPLE FIRST

namespace App\Objects;

use App\Helpers\Pets;

class User{

    public function getShortUserName(){
        $name = auth()->user()->first_name.' '.auth()->user()->last_name;
        $pet = new Pets;
        return $pet->limit($name,10);
    }
}

But somewhere I got to know that if you add Facades before your namespace, you can call the function statically even if they are non static function like this


CODE SAMPLE SECOND

namespace App\Objects;

use Facades\App\Helpers\Pets;

class User{

    public function getShortUserName(){
        $name = auth()->user()->first_name.' '.auth()->user()->last_name;
        return Pets::limit($name,10);
    }
}

Now what I want to know is I have 2 sample codes with namespace as follows

use App\Helpers\Pets;
use Facades\App\Helpers\Pets;

By adding the Facades I can call the function statically but how, that's not a valida namespace in my app What laravel doing behind the scene, I am so confused

Thank you for your time ;)

BlackXero
  • 880
  • 4
  • 17

2 Answers2

1

What you are describing is Laravels Real-Time Facades.

You can find documentation of the functionality here: https://laravel.com/docs/6.x/facades#real-time-facades

Niko Peltoniemi
  • 494
  • 3
  • 7
0

I will not enter too much in details but this is a simple explanation of what's behind the scenes when you use facades in laravel.

Let's suppose you define a custom class with some public methods:

namespace Test;

class Foo
{
    public function test()
    {
        return 'test';
    }
}

Then you have to define a facade for this class:

namespace Test1;

class BarFacade
{
    // In laravel this is called in the Facade abstract class but it is actually implemented
    // by all the facades you add across the application
    public static function getFacadeAccessor()
    {
        // In laravel you can also return a string which means that the object 
        // will be retrieved from the container.

        return new \Test\Foo();
    }

    // In laravel this method is defined in the Facade abstract class
    public static function __callStatic($method, $args)
    {
        $object = self::getFacadeAccessor();

        return call_user_func_array([$object, $method], $args);
    }
}

Then, you have to define the alias in the $aliases array of the config.app file. These aliases are parsed by laravel and registered using the php built-in function class_alias (see Illuminate/Foundation/AliasLoader.php)

class_alias('Test\Foo', 'BarFacade', true);
// You can also create an alias for the facade itself
class_alias('Test1\BarFacade', 'FooBar', true);

Then you can simply call the facades:\

var_dump(BarFacade::test());
var_dump(\Test1\BarFacade::test());
var_dump(\FooBar::test());

The results would obviously be:

string(4) "test"
string(4) "test"
string(4) "test"
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50
  • You said, We need to define a Facade for our class, But in my case I have not defined any facades for my class and yet I can access the function, anything ? – BlackXero Jan 17 '20 at 11:12