Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

Class / method precedence in X-Cart 5

 
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 12-17-2015, 12:35 PM
 
codegrunt codegrunt is offline
 

Member
  
Join Date: Dec 2015
Posts: 10
 

Default Class / method precedence in X-Cart 5

I am currently looking into migrating a number of custom modules and features from existing 4.* X-Cart installs into 5.2.

Using the Wholesale pricing module as an example, it has the following method defined:

./classes/XLite/Module/CDev/Wholesale/Model/Product.php

Code:
public function getWholesalePrice($membership) { return \XLite\Core\Database::getRepo('XLite\Module\CDev\Wholesale\Model\WholesalePrice')->getPrice( $this, $this->getWholesaleQuantity() > $this->getMinQuantity($membership) ? $this->getWholesaleQuantity() : $this->getMinQuantity($membership), $membership ); }

I have created a second test module and within it defined the same method:

./classes/XLite/Module/MyDev/MyModule/Model/Product.php

Code:
public function getWholesalePrice($membership) { return 987654321; }


After redeploying the store the result from my getWholesalePrice() method from my test module is what is being called on the front end.

How does X-Cart decide which class override should be used? Is there any way to control the precedence?

Cheers,

Ron

PS: my initial post seemed to go off into the ether (silent moderation?)
__________________
multiple versions (4 and 5)
Reply With Quote
  #2  
Old 12-17-2015, 02:08 PM
  totaltec's Avatar 
totaltec totaltec is offline
 

X-Guru
  
Join Date: Jan 2007
Location: Louisville, KY USA
Posts: 5,823
 

Default Re: Class / method precedence in X-Cart 5

Quote:
Originally Posted by codegrunt
PS: my initial post seemed to go off into the ether (silent moderation?)
You just needed the post to be approved because it is your first. Should be good from now on.
__________________
Mike White - Now Accepting new clients and projects! Work with the best, get a US based development team for just $125 an hour. Call 1-502-773-6454, email mike at babymonkeystudios.com, or skype b8bym0nkey

XcartGuru
X-cart Tutorials | X-cart 5 Tutorials

Check out the responsive template for X-cart.
Reply With Quote

The following user thanks totaltec for this useful post:
qualiteam (12-17-2015)
  #3  
Old 12-17-2015, 02:38 PM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 14,190
 

Default Re: Class / method precedence in X-Cart 5

Quote:
Originally Posted by codegrunt
How does X-Cart decide which class override should be used? Is there any way to control the precedence?

The classes are built by dev name/module alphabetically.

module/MyDev classes will be called before module/YourDev classes so YourDev will be built on top of MyDev.

To properly overwrite them you should always call parent::method first and manipulate the result the way you need it to be.

Also you should use "implements \XLite\Base\IDecorator" when extending existing classes and overwriting their methods.

If you have not seen it - .http://kb.x-cart.com/display/XDD/Developer+docs
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote

The following user thanks cflsystems for this useful post:
qualiteam (12-17-2015)
  #4  
Old 12-17-2015, 03:06 PM
 
codegrunt codegrunt is offline
 

Member
  
Join Date: Dec 2015
Posts: 10
 

Default Re: Class / method precedence in X-Cart 5

Quote:
Originally Posted by cflsystems
The classes are built by dev name/module alphabetically.

Thanks for that, at least it is predictable. X-Cart should expect a run on the Dev namespace starting with "Z".

=)

This will almost immediately be an concern for any module that needs to manipulate pricing (especially if you want a "best price" to apply) or override similar core methods that get called a lot. I don't know if there is any magic solution but I think some sort of user specified precedence would be better than hoping alphabetical works out for all cases.
__________________
multiple versions (4 and 5)
Reply With Quote
  #5  
Old 12-17-2015, 03:15 PM
  cflsystems's Avatar 
cflsystems cflsystems is offline
 

Veteran
  
Join Date: Apr 2007
Posts: 14,190
 

Default Re: Class / method precedence in X-Cart 5

Old one but I believe it is same now - https://forum.x-cart.com/showthread.php?t=69010
__________________
Steve Stoyanov
CFLSystems.com
Web Development
Reply With Quote
  #6  
Old 12-17-2015, 10:46 PM
  qualiteam's Avatar 
qualiteam qualiteam is offline
 

X-Guru
  
Join Date: Dec 2010
Posts: 6,373
 

Default Re: Class / method precedence in X-Cart 5

Yes, classes are built by dev name/module alphabetically.

Plus if you use the @LC_Dependencies tag to put your module after another one that yours depends on.
__________________
Alex Solovev,
Qualiteam

---

User manual Video tutorials X-Cart FAQ

You are welcome to press "Thanks" button
if you find this post useful

Click here to learn how to apply patches

X-Cart Extensions
Reply With Quote
Reply
   X-Cart forums > X-Cart 5 > Dev Questions (X-Cart 5)



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 03:23 AM.

   

 
X-Cart forums © 2001-2020