PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 28, 2022

[FIXED] How to solve method overloading in subclass (Declaration of ... should be compatible with )?

 April 28, 2022     inheritance, overloading, php, static, warnings     No comments   

Issue

The example should demonstrate it best:

class Generic {
    protected $a;
    protected $b;

    protected final function __construct($a,$b) {
        $this->a = $a;
        $this->b = $b;
        echo $this->a." ".$this->b;
    }

    public static function create($a,$b) {
        return new self($a,$b);
    }


}

class Wanter extends Generic {
    public static function create($b) {
        return parent::create("I want",$b);
    }
}

Generic::foo("I need","coffee!");
Wanter::foo("coffee!");

Expected output:

I need coffee!I want coffee!

Actual output:

Warning: Declaration of Wanter::create($b) should be compatible with Generic::create($a, $b) in [...][...] on line 25 I need coffee!I want coffee!

It is obvious what this should do (as it does). However I want to run this without throwing warnings of course. How to implement this without warning?


Solution

It should be obvious but the child method definition must match that of the parent, so you are missing an argument.

What I would do is this:

class Generic {
    protected $a;
    protected $b;

    protected final function __construct($a,$b) {
        $this->a = $a;
        $this->b = $b;
        echo $this->a." ".$this->b;
    }

    public static function create($a,$b) {
        return new self($b,$a); //reverse arguments
    }


}

class Wanter extends Generic {
    public static function create($a, $b="I want") {
        return parent::create($b,$a);
    }
}

Note that I changed the order of the arguments, this way the one with the default is the second argument.

You could do this just in the child, but it might be somewhat confusing, if the order is different then the parent class.

That said, something like a factory method may be more appropriate in this instance.

https://en.wikipedia.org/wiki/Factory_(object-oriented_programming)

Besides a factory pattern I am not sure how important it is for you to have the static Create method. The constructor offers more flexibility when needing things like polymorphism. For example something like this would be acceptable

abstract class Generic {
   protected $a;
   protected $b;

   protected function create($a,$b) {
        $this->a = $a;
        $this->b = $b;
        echo $this->a." ".$this->b;
    }

}

class Wanter extends Generic {
    public function __construct($a) {
        return $this->create("I want",$a);
    }
}

Then each child can define it's own constructor, with its own set of required arguments.



Answered By - ArtisticPhoenix
Answer Checked By - David Goodson (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing