-2

From extended, I want call a parent function like this

file 1 > Main Class:

class CheckoutProcessCore implements Interface
{
    public function __construct(
        Context $context,
        CheckoutSession $checkoutSession
    ) {
        $this->context = $context;
        $this->smarty = $this->context->smarty;
        $this->checkoutSession = $checkoutSession;
    }

    public function getSteps()
    {
        return $this->steps;
    }
}

file 2 > Extended Class:

class Basket extends CheckoutProcessCore
{
    protected static $basket_pid = null;

    function __construct() {
        parent::__construct();
    }

    public static function test() {
        $step = parent::getSteps();   <-- the Problem!
        [ ... ]
    }
}

How can I call getSteps from test without any errors ? What's wrong in my Code ?

Thank you

  • 2
    Your `Basket`-class extends `CheckoutProcessCore` while the `getSteps()`-method is in the class `Core` so as far as the posted code goes, your two classes aren't related in any way shape or form. – M. Eriksson Apr 28 '19 at 10:37
  • We won't be able to help you if you don't respond to our comments. – M. Eriksson Apr 28 '19 at 11:31
  • What is the error message you get? – Progman Apr 28 '19 at 16:28
  • @Magnus: I made a mistake in the Code, now I changed it. – Hardy Thiergart Apr 28 '19 at 18:22
  • @Progman: The Error shows "FatalThrowableError - Using $this when not in object context" – Hardy Thiergart Apr 28 '19 at 18:46
  • @HardyThiergart That's correct, you are in a static method, indicated by the keyword `static`. Therefore you can't use the `$this` variable. Why do you have the keyword `static` in your method when you call another method which uses/needs the `$this` variable? – Progman Apr 28 '19 at 18:59
  • @Progman: I Understand, ok. I'm using now public function unstat public static function. Now, I have a problem with the constructor. Here shows the Error "Type error: Too few arguments to function CheckoutProcessCore::__construct(), 0 passed in /var/www/onlineshop/override/classes/checkout/Basket.php on line 72 and exactly 2 expected" – Hardy Thiergart Apr 28 '19 at 19:31
  • @HardyThiergart Read the error message. The constructor of your class `CheckoutProcessCore` expect two arguments, but you wrote `parent::__construct();` to call it with 0 arguments, that's not possible. You have to call the parent constructor with two arguments as well. – Progman Apr 28 '19 at 19:44
  • @Progman: Yes, thats right, thank you. I will try to get it fixed tomorrow. My problem is right now the notice "Undefined variable: Context". I have no idea how I can get to the instance from the context.... but this tomorrow, it's to late. I count on your help. Good Night – Hardy Thiergart Apr 28 '19 at 19:57

1 Answers1

0

I'm not so sure if this might help/solve your problem. However,

  • You might have forgotten adding your files on the top of pages that might be required to do so, if you are not using autholoader/composer:

    require_once('/path/to/CheckoutProcessCore.php');
    require_once('/path/to/Basket.php');
    
  • Also, you probably are not allowed to use Interface as an identifier, for which you may look into this post, which says Interface is a reserved word. You cannot most likely use other variances of Interface, such as (interface, iNterface, etc.) as an identifier.

    require_once('/path/to/InterfaceSomeAddedNameThatYouWish.php');
    

Then, you might consider modifying:

class CheckoutProcessCore implements InterfaceSomeAddedNameThatYouWish
{
    public function __construct(
        Context $context,
        CheckoutSession $checkoutSession
    ) {
        $this->context = $context;
        $this->smarty = $this->context->smarty;
        $this->checkoutSession = $checkoutSession;
    }

    public function getSteps()
    {
        return $this->steps;
    }
}
Emma
  • 27,428
  • 11
  • 44
  • 69