Cakephp 3.x afterSave()回调在升级到PHP7.1后返回500错误

Cakephp 3.x afterSave()回调在升级到PHP7.1后返回500错误,php,cakephp,cakephp-3.0,Php,Cakephp,Cakephp 3.0,我最近将MAMP服务器更新为PHP7.1,并使用CakePHP3.x构建了一个项目。但是当我使用afterSave()回调时,我得到一个奇怪的500(内部服务器错误)错误 本文档描述了以下内容: public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options) { // some action } 当我使用PHP7.1时,它会给我一个500错误,但如果我使用PHP5.6,它会对我

我最近将MAMP服务器更新为PHP7.1,并使用CakePHP3.x构建了一个项目。但是当我使用
afterSave()
回调时,我得到一个奇怪的500(内部服务器错误)错误

本文档描述了以下内容:

public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options) {
   // some action 
}
当我使用PHP7.1时,它会给我一个500错误,但如果我使用PHP5.6,它会对我有用

现在我已经修复了PHP7.1上的这个错误,不再在函数中定义类型。但这是正确的方法吗

public function afterSave($event, $entity, $options) { 
   // some action 
}
更新:

我的错误日志显示:

2017-05-06 13:38:09错误:[TypeError]参数1传递给 Storages\Model\Table\StoragecontainerBlockElementsTable::afterSave() 必须是Storages\Model\Table\Event的实例 蛋糕\事件\给定的事件,在中调用 /Applications/MAMP/htdocs/safebend社区数据中心/community/vendor/cakephp/cakephp/src/Event/EventManager.php 第414行请求URL:/storages/blocks/dsd/ajaxAddElement 参考网址:

具有命名空间的我的表:

namespace Storages\Model\Table;

use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\ORM\TableRegistry;

class StoragecontainerBlockElementsTable extends Table { 
    public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options) { 
        // some action
    }
}
namespace Storages\Controller;

use App\Controller\AppController;
use Cake\Log\Log;
use Cake\ORM\TableRegistry;

class StoragecontainerBlocksController extends AppController { }
具有命名空间的我的控制器:

namespace Storages\Model\Table;

use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\ORM\TableRegistry;

class StoragecontainerBlockElementsTable extends Table { 
    public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options) { 
        // some action
    }
}
namespace Storages\Controller;

use App\Controller\AppController;
use Cake\Log\Log;
use Cake\ORM\TableRegistry;

class StoragecontainerBlocksController extends AppController { }

您尚未导入使用的名称,因此
Event
将引用当前名称空间,即参数将键入为
\Storages\Model\Table\Event
,而不是预期的
\Cake\Event\Event
。另外两个参数也存在同样的问题

导入类名,您应该很好:

use ArrayObject;
use Cake\Datasource\EntityInterface;
use Cake\Event\Event;

如果不这样做,任何PHP版本中都会出现错误(假设调用了该方法)。

这肯定不是正确的方法,因为您只是在解决实际问题。检查您的cake/php/server错误日志。事实上,我也这么认为。我已经用错误日志更新了我的问题。啊,谢谢!它起作用了。但奇怪的是,在较低的PHP版本(5.6)中,这被批准了。什么被批准了?@CholthiPaulTtiopic我真的不明白你的问题,你想知道的到底是什么?问题直接指向@Code,因为在5.3中引入了名称空间,您必须在使用名称空间类之前导入它们,这与他声明的已批准相反before@CholthiPaulTtiopicPHP版本5.6是否不需要添加
使用ArrayObject;使用Cake\Datasource\EntityInterface;使用Cake\Event\Event