我上了课。类的构造函数为类属性赋值。
这是定义:
class myClass
{
private $_value;
function __construct ($input)
{
$this->_value = $input;
}
function echoThat ()
{
echo $this->_value;
}
}
常规用法是:
$x = new myClass("SimpleString");
$x->echoThat();
但是我已经把这个类变成了Laravel中的一个外观类,所以它被使用了:
myClass::echoThat();
如何在Laravel Facades中使用__construct()
方法,如上例所示?
答案 0 :(得分:6)
如果您真的想使用类似Laravel外墙的话,您应该只创建一个Laravel外观。 Laravel外墙为班级提供静态界面。
这意味着要么重写你的类,以便你可以用以下任何方式传递你的字符串:
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外观访问器的任何地方实例化您的类,您可以“静态地”调用您的类的非静态方法,例如在服务提供者中:
use Illuminate\Support\ServiceProvider;
class MyClassServiceProvider extends ServiceProvider
{
public function register()
{
$this->app->bind('myclass', function()
{
return new MyClass();
});
}
}
只要使用MyClass
静态调用您的类上的方法,Laravel就会创建__callStatic
的实例。
或者不要像使用Laravel外观那样手动修改你的课程:
$x = new myClass("SimpleString");
$x->echoThat();
答案 1 :(得分:5)
你首先要明白,在Laravel的Facade并不是真正的你的课程,你必须看到它,正如名字所说,是你班级的最前沿或正面。 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');
}
}
请注意,您的类在方法register()
中实例化,现在可通过IoC容器提供给您的应用程序,因此您可以执行以下操作:
App::make('myclass')->echoThat();
现在您还可以创建Facade来使用它:
<?php namespace App\MyApp;
use Illuminate\Support\Facades\Facade as IlluminateFacade;
class ExtendedRouteFacade extends IlluminateFacade {
protected static function getFacadeAccessor()
{
return 'myclass';
}
}
Facade只有一个方法,其唯一目的是在IoC容器中返回类实例的名称。
现在您可以打开app/config/app.php
并添加ServiceProvider:
'App\MyApp\MyClassServiceProvider',
也是实际的Facade别名:
'MyClass' => 'App\MyApp\MyClassFacade',
你最好使用你的Facade:
echo MyClass::echoThat();
但请注意,您的类__constructor
的方式将始终使用相同的参数调用,在您的ServiceProvider中,这就是Facade的工作方式,但您有一些选择:
使用setter为类实例中的数据设置新值。
public function setValue($value)
{
$this->_value = $value;
}
使用Laravel自动类解析为您的班级提供参数:
function __construct (MyFooClass $foo)
{
$this->_foo = $foo;
}
并更改您的ServiceProvider,为您的班级提供无参数:
$this->app['myclass'] = $this->app->share(function()
{
return new MyClass();
});
Laravel将自动为您实例化MyFooClass
,如果它可以在您的应用程序的可用源代码中找到它,或者它是否绑定到IoC容器。
请注意,所有这些都假设您不只是在构造函数中传递字符串,IoC容器的自动解析假定您的类具有其他类依赖性并自动注入这些依赖项(依赖注入)。
如果你真的需要将字符串或单个值传递给只进行一些计算的类的构造函数,那么你真的不需要为它们创建Facade,你可以这样做:
$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();
}
}
Laravel将自动传递您班级的实例,您可以按照自己的方式使用它:
$post = new Post;
echo $post->doWhateverAction("SimpleString");
答案 2 :(得分:1)
我在本页的Facede Lavarel FW中找到了一些关于模型类的goode信息 http://laravel.com/docs/eloquent
我假设这是关于你的ploblem的主要解释,即在数据库中创建输入的方法......
// 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中没有明确的方法来使用构造函数。