Php Laravel facade _构造函数()用法
我上了一节课。类的构造函数为类属性赋值 定义如下:Php Laravel facade _构造函数()用法,php,laravel,Php,Laravel,我上了一节课。类的构造函数为类属性赋值 定义如下: class myClass { private $_value; function __construct ($input) { $this->_value = $input; } function echoThat () { echo $this->_value; } } 通常的用法是: $x = new myClass("Simple
class myClass
{
private $_value;
function __construct ($input)
{
$this->_value = $input;
}
function echoThat ()
{
echo $this->_value;
}
}
通常的用法是:
$x = new myClass("SimpleString");
$x->echoThat();
但我已将该类转换为Laravel中类的facade类型,因此使用它:
myClass::echoThat();
如何在上面的示例中使用Laravel Facades中的
\u construct()
方法?我在本页的Facede-Lavarel FW中找到了一些关于模型类的有用信息
我想这里是关于您的问题的主要解释,创建enter-in数据库的方法
// Create a new user in the database...
$user = User::create(array('name' => 'John'));
// Retrieve the user by the attributes, or create it if it doesn't exist...
$user = User::firstOrCreate(array('name' => 'John'));
// Retrieve the user by the attributes, or instantiate a new instance...
$user = User::firstOrNew(array('name' => 'John'));
下面是创建简单新实例并初始化其变量的方法:
$user = new User;
$user->name = 'John';
$user->save();
基于这一点,我假设在Lavarel中没有明确的方法来使用构造函数。如果您真的想使用类,比如使用Laravel facades,那么您应该只创建一个Laravel facade。Laravel facades为类提供了一个静态接口 这意味着您可以重写类,以便以任何其他方式传递字符串,如:
MyClassFacade::echoThat('SimpleString');
当然,您也可以修改基础类,例如使用另一个方法来传递字符串:
class MyClass
{
private $_value;
public function setValue($val)
{
$this->_value = $val;
return $this;
}
public function echoThat()
{
echo $this->_value;
}
}
然后像这样称呼它:
MyClassFacade::setValue('SimpleString')->echoThat();
如果在解析Laravel facade访问器的位置实例化类,则可以“静态地”调用类的非静态方法,例如在服务提供程序中:
use Illuminate\Support\ServiceProvider;
class MyClassServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('myclass', function()
{
return new MyClass();
});
}
}
每当使用\u callStatic
静态调用类上的方法时,Laravel将创建MyClass
的实例
或者不要像使用Laravel facade那样手动初始化类:
$x = new myClass("SimpleString");
$x->echoThat();
你首先要明白,在拉威尔,门面并不是你真正的职业,你必须看到它,正如名字所说,是你职业的前沿或正面。Facade实际上是类的服务定位器 要使用此服务定位器,您必须创建一个服务提供者,该提供者将提供Facade使用的服务:
<?php namespace App\MyApp;
use Illuminate\Support\ServiceProvider;
class MyClassServiceProvider extends ServiceProvider {
protected $defer = true;
public function register()
{
$this->app['myclass'] = $this->app->share(function()
{
return new MyClass('the values you need it to instantiate with');
});
}
public function provides()
{
return array('myclass');
}
}
现在,您还可以创建门面来使用它:
<?php namespace App\MyApp;
use Illuminate\Support\Facades\Facade as IlluminateFacade;
class ExtendedRouteFacade extends IlluminateFacade {
protected static function getFacadeAccessor()
{
return 'myclass';
}
}
另外,实际的外观别名:
'MyClass' => 'App\MyApp\MyClassFacade',
你应该善于使用你的门面:
echo MyClass::echoThat();
但是请注意,在您的ServiceProvider中,您的类\uuu构造函数
的调用方式总是使用相同的参数,这就是Facade的工作方式,但是您有一些选项:
使用setter为类实例中的数据设置新值
public function setValue($value)
{
$this->_value = $value;
}
使用Laravel自动类解析以友好方式为类提供参数:
function __construct (MyFooClass $foo)
{
$this->_foo = $foo;
}
并更改您的服务提供商,使其不向您的类提供参数:
$this->app['myclass'] = $this->app->share(function()
{
return new MyClass();
});
如果Laravel可以在应用程序的可用源代码中找到它,或者如果它绑定到IoC容器,它将自动为您实例化MyFooClass
请注意,所有这些都假定您不仅仅在构造函数中传递字符串,IoC容器的自动解析假定您的类具有其他类依赖项,并自动注入这些依赖项(依赖项注入)
如果您真的需要将字符串或单个值传递给只进行一些计算的类的构造函数,则不需要为它们创建外观,您可以:
$x = (new myClass("SimpleString"))->echoThat();
使用简单的setter:
$x = new myClass();
$x->setValue("SimpleString");
$x->echoThat();
或者,正如您已经做的,这是可以接受的:
$x = new myClass("SimpleString");
$x->echoThat();
您还可以使用IoC容器在其他类中为您实例化该类:
<?php
class Post {
private $myClass;
public function __construct(MyClass $myClass)
{
$this->myClass = $myClass;
}
public function doWhateverAction($value)
{
$this->myClass->setValue($value);
// Do some stuff...
return $this->myClass->echoThat();
}
}
好的,告诉我什么是奇怪的here@MT-Developer:它只是一个静态类。你为什么认为这是一种假象?你的解释非常清楚和直截了当,没有任何侮辱…谢谢你的代码是错误的。不能使用
$this
静态调用非静态类。@M T-Developer:在检查答案之前,检查提供的代码是否有意义是有意义的。不是这里的情况:-DI可能没有那么清楚。在本例中,myClass
应该是facade的底层类。我会尽量说清楚的。@Marcel Gwerder:你不是“不清楚”,而是不正确的。“如果你在laravel文档中寻找新的内容,你不会经常发现,这是因为几乎所有的事情都是通过facades完成的。”---这是错误的。在你继续传播你正在传播的东西之前,请检查外观是什么。它与新的
或静态类无关。谢谢!:-)
$x = new myClass("SimpleString");
$x->echoThat();
<?php
class Post {
private $myClass;
public function __construct(MyClass $myClass)
{
$this->myClass = $myClass;
}
public function doWhateverAction($value)
{
$this->myClass->setValue($value);
// Do some stuff...
return $this->myClass->echoThat();
}
}
$post = new Post;
echo $post->doWhateverAction("SimpleString");