XF 2.0 Extending the same class twice in two addons

Hello!

I need to add code to XFRM\Service\ResourceItem\Create in two addons. How should I handle that situation?
Can I extend it only once or can I do it twice?

Best regards,
Tommi
 
You can create class extensions for the same class in several addons, you could even extend the same class multiple times for one Add-on, but that doesn't make a lot of sense ;)
 
You can create class extensions for the same class in several addons, you could even extend the same class multiple times for one Add-on, but that doesn't make a lot of sense ;)
I tried extending the same class twice but I get methods only from one of them, not both of them. Am I doing something wrong?
The class in question is
\XFRM\Entity\ResourceItem
Can I use XFCP_ thingy for it?

Best regards,
Tommi
 
There are classes which are not run through the Class Proxy system. General structures like repositories, entities, controllers, etc. will all go through it though, no matter if they're from XenForo, some other add-on or your add-ons.
 
The class proxies seem to have the same namespace as the extended class. Does the class proxy system use the namespace information to determine, which class to extend, so that it does not run into problems, when there are two classes with the same name in different namespaces?
 
> Does the class proxy system use the namespace information to determine, which class to extend
No. This is specified by the developer when creating a class extension in XF backend.

> so that it does not run into problems, when there are two classes with the same name in different namespaces?
No.
 
Oh. So when I register a class extension, it will automatically create a XFCP_ classproxy for the class? I indeed was registering all my class extensions in the backend, but I did not know that it was actually creating proxy classes. Thanks for the info!
 
There are classes which are not run through the Class Proxy system. General structures like repositories, entities, controllers, etc. will all go through it though, no matter if they're from XenForo, some other add-on or your add-ons.

^ Does anyone know how to tell if the class needs a class proxy?

Is there certain directories under src/XF that do and that don't? I'm working on my first add-on now.
 
There's no way to tell unfortunately. Costs me hours every time I stumble across one to figure out why my class extension isn't working. Best way to check is to place a piece of broken code (something that'll throw a parse error, like a statement that's not terminated by a semicolon) into your extension to check if it's actually extended. It's safe to assume that your class is going through the class proxy in the first instance though. Only if you notice that your code does not do anything, then it's worth considering the possibility, that it's not.
 
There's no way to tell unfortunately. Costs me hours every time I stumble across one to figure out why my class extension isn't working. Best way to check is to place a piece of broken code (something that'll throw a parse error, like a statement that's not terminated by a semicolon) into your extension to check if it's actually extended. It's safe to assume that your class is going through the class proxy in the first instance though. Only if you notice that your code does not do anything, then it's worth considering the possibility, that it's not.

Thank you for your reply. I really appreciate it.
 
I have a quick questions again guys. Why is it that most professional add-ons where I'm looking at their code, specifically their entity code do not use the XFCP_ in their add-ons? The majority of them just extend the class without it. I haves seen a couple use it but the main developers which add-ons I have bought from don't even use it? So I'm now assuming it's optional but not required? @S Thomas
 
It's not optional and it's not good because it could cause a lot of trouble. Directly extending an entity means possible incompatibility issues with other addons. Imagine two addons extending the same class and same method. Well, there you go. Which method do you think would be called? In any case, not both.
 
It's not optional and it's not good because it could cause a lot of trouble. Directly extending an entity means possible incompatibility issues with other addons. Imagine two addons extending the same class and same method. Well, there you go. Which method do you think would be called? In any case, not both.

Now that I have viewed several add-ons I purchased. [TH]Question & Answer forums, [TH] Bookmarks, [BD] Medal all extend "Entity" directly in their Entity files. But nothing else. It only extends the class "Entity" directly. So if I'm extending Entity only then it's okay to do it without XFCP_?

This leads me to believe that certain core classes don't need the XFCP_ prefix or base classes but classes lower in the chain would need it.

It's not just 1 add-on where I'm seeing the same extension of "entity" without it. I'm seeing it on almost all of the professional core add-ons for Xenforo 2.

This will definitely help other people so any response to this is valued greatly.
 
Are you talking about the abstract class "Entity" or a specific entity like \XF\Entity\Thread? If it's the abstract entity (\XF\Mvc\Entity\Entity), there's nothing wrong with that, that's how you should create new structures. You don't proxy abstract classes, you proxy parent classes, e.g. those, who have already extended an abstract class. And as @Lukas W. recently said somewhere, there are exceptions to that aswell.
 
Are you talking about the abstract class "Entity" or a specific entity like \XF\Entity\Thread? If it's the abstract entity (\XF\Mvc\Entity\Entity), there's nothing wrong with that, that's how you should create new structures. You don't proxy abstract classes, you proxy parent classes, e.g. those, who have already extended an abstract class. And as @Lukas W. recently said somewhere, there are exceptions to that aswell.

Yes it was the an abstract entity. Thanks that makes so much sense now. I didn't know that. Now I know. Very informative by the way. I really appreciate your information.
 
Top Bottom