<?php
# model.php
namespace Model\TestModel;
class User
{
public function get()
{
return '爱因斯坦';
}
}
<?php
# run.php
use Model\TestModel\User;
require __DIR__ . '/model.php';
$user = new User();
echo $user->get();
I want to implement the same usage as Laravel Facades, load.php
How to write it?
<?php
# run.php
require __DIR__ . '/load.php';
echo \User::get();
扔个三星炸死你2017-06-21 10:12:55
You need to implement a Facade object yourself to proxy all method calls to the real object.
class UserFacade
{
private $user;
public function __constructor()
{
$this->user = new User();
}
public function __callStatic($name, $arguments)
{
return call_user_func_array(array($this, $name), $arguments);
}
}
$user UserFacade::get();
Any static method call to UserFacade will be proxied to User. __callStatic
is triggered when there is a static call. The passed parameter $name is the method name of UserFacade::get
static call, and $arguments is the array of parameters.
Laravel official description Facade is a concise and easy-to-remember class call. Laravel basically provides the Facade class for its features. At the level of business development, I do not recommend the design method of static class calling. I will open a separate post to discuss this.
Reference
https://laravel.com/docs/5.4/…
http://php.net/manual/en/lang...
http://php.net/manual/en/func...