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

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计