php 7.1用特征在蛋糕php 2申请中使用碰撞错误
我正在在运行PHP 7.1的Cake PHP 2应用程序中进行编码,由于业务限制,我无法升级PHP版本或蛋糕版本,我正在构建一个具有特征
的自定义“模块”系统文件夹使用PHP特征,我的控制器正在调用这些特征,每个特征都包含其他特征,因为这意味着我很容易重复使用代码。
我遇到的问题是,直到PHP 7.3之前,PHP对“使用其他特征方法的碰撞”造成了致命错误,现在我不记得每个性状中都有任何重复的方法,有没有办法让我悄悄关闭此问题。特定的PHP错误或稍微修改我的代码,以便我不必在所有特征中以不同的方式重命名每个功能/方法?
我的仪表板控制器是
<?php
App::uses('AppController', 'Controller');
App::uses('CakeEvent', 'CakeEventManager', 'Event');
// traits
App::uses('Settings', 'modules/QueueManagerModule/Traits');
App::uses('Presenters', 'modules/QueueManagerModule/Traits');
App::uses('Statistics', 'modules/QueueManagerModule/Traits');
App::uses('Jobs', 'modules/QueueManagerModule/Traits');
App::uses('Dates', 'modules/QueueManagerModule/Traits');
class QueueController extends AppController
{
use Settings, Presenters, Statistics, Jobs, Dates;
/**
* View: Queue/dashboard
*/
public function dashboard()
{
// dashboard
}
}
我称为jobs
的7个特征中的一个:
<?php
App::uses('ClassRegistry', 'Utility');
// Traits
App::uses('Helpers', 'modules/QueueManagerModule/Traits');
App::uses('Settings', 'modules/QueueManagerModule/Traits');
trait Jobs {
use Helpers, Settings;
// methods
}
I'm coding within a Cake PHP 2 application running PHP 7.1, I cannot upgrade the PHP version or Cake version due to business constraints, I'm building out a custom "module" system which has a Traits
folder using PHP traits, my controller is calling these Traits and each Trait includes other traits as this means I'm easily able to reuse code.
The issue I have is that up until PHP 7.3, PHP throws a fatal error for "collissions with other trait methods", now I don't recall having any duplicate method names in each trait, is there a way I can quietly turn off this specific PHP error or slightly modify my code so that I don't have to rename every single function/method differently in all of the traits that I have?
My dashboard controller is
<?php
App::uses('AppController', 'Controller');
App::uses('CakeEvent', 'CakeEventManager', 'Event');
// traits
App::uses('Settings', 'modules/QueueManagerModule/Traits');
App::uses('Presenters', 'modules/QueueManagerModule/Traits');
App::uses('Statistics', 'modules/QueueManagerModule/Traits');
App::uses('Jobs', 'modules/QueueManagerModule/Traits');
App::uses('Dates', 'modules/QueueManagerModule/Traits');
class QueueController extends AppController
{
use Settings, Presenters, Statistics, Jobs, Dates;
/**
* View: Queue/dashboard
*/
public function dashboard()
{
// dashboard
}
}
And one, of 7 traits I have called Jobs
:
<?php
App::uses('ClassRegistry', 'Utility');
// Traits
App::uses('Helpers', 'modules/QueueManagerModule/Traits');
App::uses('Settings', 'modules/QueueManagerModule/Traits');
trait Jobs {
use Helpers, Settings;
// methods
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这不是这里的问题。
问题是您正在这样做:
然后是:
作业特质使用
settings
特征的事实,queuecontroller
class> queuecontroller同时使用作业
和设置
特征正在创建问题。更一般而言,应用已经使用其他特征的性状,然后直接包括其他特征也会引起问题。
考虑这个更简单的示例:
如果执行它,您会看到丢弃相同的错误:
没有办法“悄悄关闭此特定的PHP错误”,这不是
警告
或<代码>注意您可以简单地忽略。这是一个致命错误
将会显示始终显示,唯一解决该代码的方法是更改代码,以便您不将同一特质应用于类别(即使是您的情况,甚至间接地)。因此,四个您的示例,解决方案是从
queuecontroller
类中删除settings
特征,因为它已经包含在jobs trait(和对于所有在自己内部使用特征的其他特征)做同样的事情):
This is not the issue here.
The issue is that you're doing this:
and then this:
The fact that the
Jobs
trait uses theSettings
trait, and theQueueController
class uses both theJobs
andSettings
traits is creating the issue.More generally, applying a trait that is already using some other trait, and then including that other trait directly as well causes the issue.
Consider this simpler example:
If you execute it, you will see that the same error is thrown:
There is no way to "quietly turn off this specific PHP error", this isn't a
warning
ornotice
that you can simply ignore. It's afatal error
that will always show up, and the only way to solve it is by changing your code so that you don't apply the same trait twice to a class (even indirectly, as in your case).So, four your example, the solution is to remove the
Settings
trait from theQueueController
class, because it's already included in theJobs
trait (and do the same for all the other traits that use traits inside themselves):