【问题标题】:laravel abstract method fatal exception errorlaravel 抽象方法致命异常错误
【发布时间】:2013-09-19 21:40:29
【问题描述】:

我正在使用 laravel 4 中包含的 User 类。我正在尝试存储一个属于用户的新问题,并且用户需要登录才能创建。当我调用问题控制器操作存储时,我收到以下错误

Class User contains 2 abstract methods and must therefore be declared abstract or implement the remaining methods (Illuminate\Auth\UserInterface::getAuthPassword, Illuminate\Auth\Reminders\RemindableInterface::getReminderEmail)

我已经阅读了一些关于 php 中的抽象方法的内容,虽然我并不完全理解它们,但错误本身为问题提供了两种解决方案,声明实现剩余方法的类抽象。我猜测,由于这是 laravel 附带的模型类,因此正确的解决方案不是将其声明更改为抽象,而是实现其余方法。在这种情况下如何正确执行此操作并继续前进?

用户模型

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends BaseModel implements UserInterface, RemindableInterface {

    protected $guarded = [];

    public static $rules = array(
        'username' => 'required|unique:users|alpha_dash|min:4',
        'password' => 'required|alpha_num|between:4,8|confirmed',
        'password_confirmation'=>'required|alpha_num|between:4,8' 
        );

    public function Questions($value='')
    {
        return $this->hasMany('Question');

    }

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'users';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('password');

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->password;
    }

    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail()
    {
        return $this->email;
    }

}

问题控制器

/**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function postStore()
    {
        $validation = Question::validate(Input::all());

        if($validation->passes()) {
            Question::create(array(
                'question'=>Input::get('question'),
                'user_id'=>Auth::user()->id
            ));

            return Redirect::Route('home')
            ->with('message', 'Your question has been posted.');

        } else {
            return Redirect::to('user/register')->withErrors($validation)
            ->withInput();
        }
    }

edit 1:错误消息包括'(Illuminate\Auth\UserInterface::getAuthPassword, Illuminate\Auth\Reminders\RemindableInterface::getReminderEmail)' 这两种方法在我的 user.php 中作为 publice 函数,如您在上面看到的,那么我需要做其他事情来“实施”它们吗?

编辑2:

Laravel Src 用户接口类

<?php namespace Illuminate\Auth;

interface UserInterface {

    /**
     * Get the unique identifier for the user.
     *
     * @return mixed
     */
    public function getAuthIdentifier();

    /**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword();

}

laravel src RemindableInterface 类

<?php namespace Illuminate\Auth\Reminders;

interface RemindableInterface {

    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail();

}

编辑3:

php.ini 相关报错

; error_reporting
;   Default Value: E_ALL & ~E_NOTICE
;   Development Value: E_ALL | E_STRICT
;   Production Value: E_ALL & ~E_DEPRECATED

error_reporting = E_ALL 

; Eval the expression with current error_reporting().  Set to true if you want
; error_reporting(0) around the eval().
; http://php.net/assert.quiet-eval
;assert.quiet_eval = 0

基础模型类

<?php

class Basemodel extends Eloquent {

    public static function validate($data) {

        return Validator::make($data, static::$rules);
    }
}


?>

编辑 4;

在给出错误时添加正确的模型类以及修复后的情况

<?php

use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;

class Question extends BaseModel implements UserInterface, RemindableInterface {

    protected $guarded = [];

    public static $rules = array(
            'questions'=>'required|min:10|max:255',
            //'solved'=>'in:0,1',
        );

    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'questions';

    /**
     * The attributes excluded from the model's JSON form.
     *
     * @var array
     */
    protected $hidden = array('');

    /**
     * Get the unique identifier for the question.
     *
     * @return mixed
     */
    public function getAuthIdentifier()
    {
        return $this->getKey();
    }

    public function user()
    {
        return $this->belongsTo('User');
    }

}

添加这个来修复

/**
     * Get the password for the user.
     *
     * @return string
     */
    public function getAuthPassword()
    {
        return $this->password;
    }

    /**
     * Get the e-mail address where password reminders are sent.
     *
     * @return string
     */
    public function getReminderEmail()
    {
        return $this->email;
    }

【问题讨论】:

    标签: php laravel laravel-4


    【解决方案1】:

    也许用一个例子来回答这个问题是最简单的。假设我有以下课程:

    abstract class ClassB {
        public abstract function foo();
    }
    
    class ClassA extends ClassB {}
    
    $a = new ClassA();
    

    运行此代码将导致以下错误:

    Fatal error: Class ClassA contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (ClassB::foo)
    

    这意味着我在ClassA 中缺少foo()(在ClassB 中定义)的实现。抽象方法只能在抽象类中定义,这意味着任何非抽象派生类都必须公开完整的实现——ClassA 在这种情况下不会。上面的例子可以通过把ClassA改成

    来解决
    class ClassA extends ClassB {
    
        // implementation of abstract ClassB::foo().
        public function foo() {
            echo 'Hello!';
        }
    }
    

    回到你的例子。你的User 类扩展了BaseModel。根据BaseModel 是否扩展了另一个抽象类,它将包含两个定义为abstract 的方法,而您的User 类是缺失的。您需要找到这些方法 - 我的错误消息明确告诉我我缺少什么 - 并在 User 中实现它们。

    【讨论】:

    • 您能否将这两种方法的完整签名添加到您的问题中?您必须确保实现的签名匹配(具有相同或更高的可见性)。
    • 我不完全确定你所说的完整签名是什么意思抱歉,但我猜了一下,看看编辑 2
    • 这很奇怪,尤其是getAuthIdentifier() 方法似乎很好。是否在 -1 上报告错误?如果您将 User 更改为不扩展 BaseModel 并尝试仅实例化它,您会看到任何错误吗?
    • 我添加了 php.ini 中我能找到的与 error_reporting 相关的部分。如果类 dosnt 扩展 basemodel 它不会扩展 eloquent orm,这将完全破坏应用程序。
    • 我承认自己没有想法。也许我没有意识到 OO PHP 的复杂性。 getAuthIdentifier()getAuthPassword()之间一定有区别,但是我找不到...
    猜你喜欢
    • 1970-01-01
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 2016-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多