XF 2.0 The correct way to extend the same class in two separate addons

entelechy

Active member
I'm extending XFCP_Forum in two separate small addons, both of which work fine individually, but when both are live the effects of one of them are being negated all together.

I've got a few questions about this process:

  1. If I want to replace a single return statement with a condition that could return either of two things, how can that be done through a class extension in an addon? Currently I'm overwriting the entire method with the same method from the core code, and one line being changed, but I know there's got to the a correct way of doing that where I'm not overwriting the entire method.

  1. If I'm using.. class Forum extends XFCP_Forum .. in one addon called BusinessButtons, how would I extend the extended class in the first addon, in the second addon. I've tried to use this in the second addon.. class Forum extends ListingPages\Pub\Controller\Forum

..but I get an error: Fatal error: Class 'BusinessesButtons\Pub\Controller\ListingPages\Pub\Controller\Forum' not found in /var/www/stadivm.com/html/src/addons/BusinessesButtons/Pub/Controller/Forum.php on line 7

  1. Is there any issue with creating a chain of extended classes that may all work on the same class in different ways, and extend the modifications made from the previous addon extension(s), or is it better to try to only extend the core class and then work on that? Why?
 
Sounds like you may need to try a different approach completely.

Is it possible for you to post some code?
 
You extend the base class twice, that's what the class proxy is there for in the first place.

8XL0p7y.png

iAtcPKx.png



Code:
namespace ThemeHouse\Trending\XF\Entity;

class User extends XFCP_User
{
//...
}

Code:
namespace ThemeHouse\Bookmarks\XF\Entity;

class User extends XFCP_User
{
//...
}
 
Currently I'm overwriting the entire method with the same method from the core code, and one line being changed, but I know there's got to the a correct way of doing that where I'm not overwriting the entire method.
In most cases you should call parent::methodName($parameter); (otherwise you would completely overwrite / erase the parent class's method).

So you can chain multiple class extensions and every extensions adds something, but the base class's functionality remains.

So e.g.
PHP:
public function someMethod($parameter)
{
    if ($something == 'happens')
    {
        return $youOutput;
    }

    return parent::someMethod($parameter);
}

  1. If I'm using.. class Forum extends XFCP_Forum .. in one addon called BusinessButtons, how would I extend the extended class in the first addon, in the second addon. I've tried to use this in the second addon.. class Forum extends ListingPages\Pub\Controller\Forum
..but I get an error: Fatal error: Class 'BusinessesButtons\Pub\Controller\ListingPages\Pub\Controller\Forum' not found in /var/www/stadivm.com/html/src/addons/BusinessesButtons/Pub/Controller/Forum.php on line 7
The error here is that you have to prefix you class namespace with a Backslash: class Forum extends \ListingPages\Pub\Controller\Forum (or whatever the namespace is). But better use the approach with calling the parent method!
 
Last edited:
Top Bottom