0

I'm using magento 2.2 and I'm trying to extend a class, more exactly this one: \Magento\SalesRule\Model\Utility

And I'm facing an error:

Declaration of MagePal\CouponCodeExtraValidation\Model\SalesRule\ExtraValidation::canProcessRule(Magento\SalesRule\Model\Rule $rule, Magento\Quote\Model\Quote\Address $address) should be compatible with Magento\SalesRule\Model\Utility::canProcessRule($rule, $address) in /Applications/magento-2.2.1-1/apps/magento/htdocs/app/code/MagePal/CouponCodeExtraValidation/Model/SalesRule/ExtraValidation.php on line 0

This is how my class looks like:

<?php
/**
 * Copyright © 2015 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace MagePal\CouponCodeExtraValidation\Model\SalesRule;

class ExtraValidation extends \Magento\SalesRule\Model\Utility
{
    public function canProcessRule(
        \Magento\SalesRule\Model\Rule $rule, 
        \Magento\Quote\Model\Quote\Address $address
    ){
        parent::canProcessRule($rule, $address);
    }
}

What am I doing wrong?

Thank you

1

You should define your method like this:

public function canProcessRule($rule, $address)
{
    parent::canProcessRule($rule, $address);
}

to make it compatible with the parent method.

  • thanks! it's ok now. could you explain why sometimes I need to include that path before the variable and sometimes not? – Cosmin Apr 16 '18 at 13:17
  • @Cosmin That's purely a php question. If you have a class and you are defining a new class which does not define by its parent class, then you can type hint as you done here. But if you are redefining a method which already exists in the parent class, then you should define it exactly like in the parent class. – Rajeev K Tomy Apr 16 '18 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.