dongshao8471 2015-05-22 21: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-23 04: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) {
            //
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?