dongshao8471 2015-05-22 13:10
浏览 47
已采纳

类型提示雄辩模型

I am trying to write good code, and part of that is type hinting to make it easier for work down the line and to force expectations.

This may seem a little contrived, but its more of a proof of concept for me.

I am writing a class to take a TSV file split on tabs and insert into my Model. In my constructor I was asking for:

Illuminate\Database\Eloquent\Model

To which I passed:

new \App\Model()

And finally the error response of:

instance of App\Model given

Clearly I have done something wrong, but I do not want to force usage of App\Model, how can I generically ask for an eloquent model?

Edit for more information:

To make it more clear, I am using Laravel 5, the models are created via artisan make:model. The constructor is as follows:

function __construct ($resource, Illuminate\Database\Eloquent\Model $model, $skip = 0)

And the Model I am using (for my movie table) is:

use Illuminate\Database\Eloquent\Model;

class Movie extends Model {
  • 写回答

1条回答 默认 最新

  • dreamone5156 2015-05-22 20:55
    关注

    In your type hint, preface the FQCN with a backslash:

    function __construct ($resource, \Illuminate\Database\Eloquent\Model $model, $skip = 0)
    

    Either that, or add the use statement to your class:

    use Illuminate\Database\Eloquent\Model;
    
    class MyClass {
        function __construct ($resource, Model $model, $skip = 0) {
            //
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部