laravel 抽象方法致命异常错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18816161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel abstract method fatal exception error
提问by Mark
I am using the User class included with laravel 4. I am trying to store a new question that belongs to the user and the user needs to be logged in to create. when I call the questions controller action store I get the following error
我正在使用 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)
I have read a bit on abstract methods in php and while I don't completely understand them the error itself gives two solutions to the problem, declare the class abstract of implement the remaing methods. I am guessing that since this is the model class that ships with laravel that the correct solution is not to change its declaration to abstract but to implement the remaining methods. How do I do this correctly in this case and going forward?
我已经阅读了一些关于 php 中的抽象方法的内容,虽然我并不完全理解它们,但错误本身为问题提供了两种解决方案,声明实现剩余方法的类抽象。我猜测,由于这是 Laravel 附带的模型类,因此正确的解决方案不是将其声明更改为抽象,而是实现其余方法。在这种情况下我如何正确地做到这一点并继续前进?
User Model
用户模型
<?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;
}
}
Questions Controller
问题控制器
/**
* 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: The error message includes '(Illuminate\Auth\UserInterface::getAuthPassword, Illuminate\Auth\Reminders\RemindableInterface::getReminderEmail)' these two methods are in my user.php as publice functions as you can see above, so do I need to do something else to 'implement' them?
编辑 1:错误消息包括 '(Illuminate\Auth\UserInterface::getAuthPassword, Illuminate\Auth\Reminders\RemindableInterface::getReminderEmail)' 这两个方法在我的 user.php 中作为 publice 函数,如上所示,也是如此我需要做其他事情来“实施”它们吗?
edit 2:
编辑2:
Laravel Src UserInterface Class
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 class
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();
}
edit 3:
编辑3:
php.ini related to error reporting
错误报告相关的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
basemodel class
基础模型类
<?php
class Basemodel extends Eloquent {
public static function validate($data) {
return Validator::make($data, static::$rules);
}
}
?>
edit 4;
编辑 4;
Adding correct model class as it was when giving the error and how it is now with the fix
添加正确的模型类,因为它在给出错误时以及现在如何修复
<?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');
}
}
add this to fix
添加这个来修复
/**
* 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;
}
回答by George Brighton
Perhaps it's easiest to answer this with an example. Say I have the following classes:
也许用一个例子来回答这个问题最容易。假设我有以下课程:
abstract class ClassB {
public abstract function foo();
}
class ClassA extends ClassB {}
$a = new ClassA();
Running this code will result in the following error:
运行此代码将导致以下错误:
Fatal error: Class ClassA contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (ClassB::foo)
This means I'm missing an implementation of foo()
(defined in ClassB
) in ClassA
. Abstract methods can only be defined in abstract classes, and mean that any non-abstract derived class must expose a full implementation - which ClassA
doesn't in this case. The above example can be fixed by changing ClassA
to
这意味着我失踪的实现foo()
(定义在ClassB
中)ClassA
。抽象方法只能在抽象类中定义,这意味着任何非抽象派生类都必须公开完整的实现 -ClassA
在这种情况下不会。上面的例子可以通过更改ClassA
为
class ClassA extends ClassB {
// implementation of abstract ClassB::foo().
public function foo() {
echo 'Hello!';
}
}
Back to your example. Your User
class extends BaseModel
. Depending on whether BaseModel
extends another abstract class, it will contain two methods defined as abstract
that your User
class is missing. You need to find these methods - my error message explicitly told me what I was missing - and implement them in User
.
回到你的例子。您的User
课程扩展了BaseModel
. 根据是否BaseModel
扩展另一个抽象类,它将包含两个定义为abstract
您的User
类缺失的方法。你需要找到这些方法——我的错误信息明确地告诉我我遗漏了什么——并在User
.