简体   繁体   English

如何在php中扩展多个类?

[英]How to extends multiple classes, in php?

I would like to create a form in my "contactus" page. 我想在“ contactus”页面上创建一个表单。 I'm following this tutorial . 我正在关注本教程 I have to extends the sfFrom class to use sfWidget, but in my code I've already extends BaseController. 我必须扩展sfFrom类以使用sfWidget,但是在我的代码中,我已经扩展了BaseController。

Is there any solution to do this without making a new class? 有没有解决方案,而无需上新课?

class InfoController extends BaseController
{
    /**
     *
     * @Route("/contactus", name="info_contactus_page")
     * @Template()
     */
    public function contactusAction()
    {
        return array();
    }
}

You're doing all wrong. 你做错了

symfony 1 is completely different than Symfony 2 (check this article: How Symfony2 differs from symfony1 ). symfony 1与Symfony 2 完全不同 (请检查本文: Symfony2与symfony1有何不同 )。 You follow a tutorial from sf1 to create form in sf2. 您遵循sf1的教程在sf2中创建表单。

You should instead try to find resources for sf2, like : 相反,您应该尝试查找sf2的资源,例如:

And since you're french, I recommend you to read these tutorials: 由于您是法国人,因此建议您阅读以下教程:

Then, when you will have read these tutorials, you can come back here to ask a new question about a problem you got when implementing a form. 然后,当您阅读了这些教程之后,可以回到这里提出有关实现表单时遇到的问题的新问题。

use composition or interfaces. 使用组合或接口。

In your case composition is solution. 在您的情况下,组成就是解决方案。

PHP does not allow multiple inheritance PHP不允许多重继承

If you can use PHP5.4, use traits: http://php.net/manual/en/language.oop5.traits.php 如果可以使用PHP5.4,请使用特征: http ://php.net/manual/en/language.oop5.traits.php

<?php 

trait SomeOtherControllerTrait {
    function SomethingSomethingSomething() { /* Something */ }
}

class InfoController extends BaseController {
    use SomeOtherControllerTrait;
    /* ... */
}

They allow you to get similar result to multiple inheritence by using composition. 它们使您可以通过使用合成来获得与多重继承相似的结果。

EDIT: Since the tags underneath the question changed to Symfony2: You don't need the multiple inheritance since Symfony2 handled this situation in a different way. 编辑:由于该问题下面的标签更改为Symfony2:您不需要多重继承,因为Symfony2以不同的方式处理了这种情况。 The accepted answer described it well. 公认的答案描述得很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM