如何使用已存在的Validator类从命名空间调用Validator [英] How do I call Validator from a namespace with an already existing Validator class

查看:158
本文介绍了如何使用已存在的Validator类从命名空间调用Validator的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在phpspec中测试一个调用Laravel的Validator::make函数的函数( http://laravel.com/docs/4.2/validation )

I'm trying to test a function in phpspec which calls Laravel's Validator::make function (http://laravel.com/docs/4.2/validation)

但是,我试图从已经使用Validator类名的命名空间中调用相同的函数.如何调用文档中所述的功能?

However, I'm trying to call that same function from a namespace where the Validator class name is already taken. How can I call that function described in the docs?

失败的解决方案:

尝试1

return \Illuminate\Validation\Validator::make($values,$rules);

给我

Call to undefined method Illuminate\Validation\Validator::make()

尝试2

return \Illuminate\Validation\Factory::make($values,$rules);

给我

Using $this when not in object context in /vendor/laravel/framework/src/Illuminate/Validation/Factory.php on line 92. Factory

尝试3

use \Validator;

给我

Cannot declare class Isoform\Validator because the name is already in use

尝试4

use \Validator as DefaultValidator;

给我

Class 'DefaultValidator' not found

推荐答案

return \Illuminate\Support\Facades\Validator::make($values,$rules);

这将导致phpspec错误,但无法避免.尽管Validator::make看起来像一个静态函数-在幕后它实际上是在返回一个实例.因为我使用的是phpspec,所以未创建该实例,因此会出现错误.

This will cause errors in phpspec, but that cannot be avoided. Although Validator::make looks like a static function - behind the scenes it is really returning an instance. Because I was using phpspec, that instance was not created hence the error.

这篇关于如何使用已存在的Validator类从命名空间调用Validator的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆