php 7.1用特征在蛋糕php 2申请中使用碰撞错误

发布于 2025-01-24 07:08:45 字数 1335 浏览 4 评论 0原文

我正在在运行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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

未央 2025-01-31 07:08:45

我不记得每个特征中都有任何重复的方法

这不是这里的问题。
问题是您正在这样做:

trait Jobs {
    use Helpers, Settings;
}

然后是:

class QueueController extends AppController
{
    use Settings, Presenters, Statistics, Jobs, Dates;
}

作业特质使用settings特征的事实,queuecontroller class> queuecontroller同时使用作业设置特征正在创建问题。

更一般而言,应用已经使用其他特征的性状,然后直接包括其他特征也会引起问题。

考虑这个更简单的示例:

<?php

trait FooTrait {
    function foo()
    {}
}

trait BarTrait {
    function bar()
    {}
}

trait BazTrait {
    use FooTrait, BarTrait;

    function baz()
    {}
}

class Test {
    use FooTrait, BarTrait, BazTrait;

    function doSomething()
    {}
}

$test = new Test;

var_dump(get_class_methods($test));

如果执行它,您会看到丢弃相同的错误:

Fatal error: Trait method foo has not been applied, because there are collisions with other trait methods on Test in /in/H54W0 on line 20

没有办法“悄悄关闭此特定的PHP错误”,这不是警告或<代码>注意您可以简单地忽略。这是一个致命错误将会显示始终显示,唯一解决该代码的方法是更改​​代码,以便您不将同一特质应用于类别(即使是您的情况,甚至间接地)。

因此,四个您的示例,解决方案是从queuecontroller类中删除settings特征,因为它已经包含在jobs trait(和对于所有在自己内部使用特征的其他特征)做同样的事情):

<?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 Presenters, Statistics, Jobs, Dates;

    /**
    * View: Queue/dashboard
    */
    public function dashboard()
    {
        // dashboard
    }

}
<?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 don't recall having any duplicate method names in each trait

This is not the issue here.
The issue is that you're doing this:

trait Jobs {
    use Helpers, Settings;
}

and then this:

class QueueController extends AppController
{
    use Settings, Presenters, Statistics, Jobs, Dates;
}

The fact that the Jobs trait uses the Settings trait, and the QueueController class uses both the Jobs and Settings 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:

<?php

trait FooTrait {
    function foo()
    {}
}

trait BarTrait {
    function bar()
    {}
}

trait BazTrait {
    use FooTrait, BarTrait;

    function baz()
    {}
}

class Test {
    use FooTrait, BarTrait, BazTrait;

    function doSomething()
    {}
}

$test = new Test;

var_dump(get_class_methods($test));

If you execute it, you will see that the same error is thrown:

Fatal error: Trait method foo has not been applied, because there are collisions with other trait methods on Test in /in/H54W0 on line 20

There is no way to "quietly turn off this specific PHP error", this isn't a warning or notice that you can simply ignore. It's a fatal 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 the QueueController class, because it's already included in the Jobs trait (and do the same for all the other traits that use traits inside themselves):

<?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 Presenters, Statistics, Jobs, Dates;

    /**
    * View: Queue/dashboard
    */
    public function dashboard()
    {
        // dashboard
    }

}
<?php

App::uses('ClassRegistry', 'Utility');

// Traits
App::uses('Helpers', 'modules/QueueManagerModule/Traits');
App::uses('Settings', 'modules/QueueManagerModule/Traits');

trait Jobs {
    use Helpers, Settings;

    // methods
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文