PHP: How to solve method overloading in subclass (Declaration of … should be compatible with )?
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?
php inheritance static warnings overloading
add a comment |
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?
php inheritance static warnings overloading
public static function create($a,$b)
your missing an argument for the child method.
– ArtisticPhoenix
Nov 13 '18 at 17:25
Yeah but thats the point. That argument is not needed andcreate()
is possible with a single argument, because the subclass "knows" argument $a.
– Blackbam
Nov 13 '18 at 17:29
2
Making child classes more restrictive than the classes they extend is a violation of the Liskov substitution principle. Traditional method overriding isn't the pattern you want to use for something like this.
– iainn
Nov 13 '18 at 17:30
@iainn Thanks thats good to know.
– Blackbam
Nov 13 '18 at 17:34
add a comment |
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?
php inheritance static warnings overloading
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?
php inheritance static warnings overloading
php inheritance static warnings overloading
asked Nov 13 '18 at 17:16
BlackbamBlackbam
5,188124475
5,188124475
public static function create($a,$b)
your missing an argument for the child method.
– ArtisticPhoenix
Nov 13 '18 at 17:25
Yeah but thats the point. That argument is not needed andcreate()
is possible with a single argument, because the subclass "knows" argument $a.
– Blackbam
Nov 13 '18 at 17:29
2
Making child classes more restrictive than the classes they extend is a violation of the Liskov substitution principle. Traditional method overriding isn't the pattern you want to use for something like this.
– iainn
Nov 13 '18 at 17:30
@iainn Thanks thats good to know.
– Blackbam
Nov 13 '18 at 17:34
add a comment |
public static function create($a,$b)
your missing an argument for the child method.
– ArtisticPhoenix
Nov 13 '18 at 17:25
Yeah but thats the point. That argument is not needed andcreate()
is possible with a single argument, because the subclass "knows" argument $a.
– Blackbam
Nov 13 '18 at 17:29
2
Making child classes more restrictive than the classes they extend is a violation of the Liskov substitution principle. Traditional method overriding isn't the pattern you want to use for something like this.
– iainn
Nov 13 '18 at 17:30
@iainn Thanks thats good to know.
– Blackbam
Nov 13 '18 at 17:34
public static function create($a,$b)
your missing an argument for the child method.– ArtisticPhoenix
Nov 13 '18 at 17:25
public static function create($a,$b)
your missing an argument for the child method.– ArtisticPhoenix
Nov 13 '18 at 17:25
Yeah but thats the point. That argument is not needed and
create()
is possible with a single argument, because the subclass "knows" argument $a.– Blackbam
Nov 13 '18 at 17:29
Yeah but thats the point. That argument is not needed and
create()
is possible with a single argument, because the subclass "knows" argument $a.– Blackbam
Nov 13 '18 at 17:29
2
2
Making child classes more restrictive than the classes they extend is a violation of the Liskov substitution principle. Traditional method overriding isn't the pattern you want to use for something like this.
– iainn
Nov 13 '18 at 17:30
Making child classes more restrictive than the classes they extend is a violation of the Liskov substitution principle. Traditional method overriding isn't the pattern you want to use for something like this.
– iainn
Nov 13 '18 at 17:30
@iainn Thanks thats good to know.
– Blackbam
Nov 13 '18 at 17:34
@iainn Thanks thats good to know.
– Blackbam
Nov 13 '18 at 17:34
add a comment |
1 Answer
1
active
oldest
votes
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.
plus one purely for It should be obvious
– RiggsFolly
Nov 13 '18 at 17:34
Yea problems like this are often an indicator that the wrong pattern is being used for the class structure. If it was just he constructor their wouldn't be so much of an issue
– ArtisticPhoenix
Nov 13 '18 at 17:38
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286357%2fphp-how-to-solve-method-overloading-in-subclass-declaration-of-should-be-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
plus one purely for It should be obvious
– RiggsFolly
Nov 13 '18 at 17:34
Yea problems like this are often an indicator that the wrong pattern is being used for the class structure. If it was just he constructor their wouldn't be so much of an issue
– ArtisticPhoenix
Nov 13 '18 at 17:38
add a comment |
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.
plus one purely for It should be obvious
– RiggsFolly
Nov 13 '18 at 17:34
Yea problems like this are often an indicator that the wrong pattern is being used for the class structure. If it was just he constructor their wouldn't be so much of an issue
– ArtisticPhoenix
Nov 13 '18 at 17:38
add a comment |
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.
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.
edited Nov 13 '18 at 17:42
answered Nov 13 '18 at 17:30
ArtisticPhoenixArtisticPhoenix
16k11223
16k11223
plus one purely for It should be obvious
– RiggsFolly
Nov 13 '18 at 17:34
Yea problems like this are often an indicator that the wrong pattern is being used for the class structure. If it was just he constructor their wouldn't be so much of an issue
– ArtisticPhoenix
Nov 13 '18 at 17:38
add a comment |
plus one purely for It should be obvious
– RiggsFolly
Nov 13 '18 at 17:34
Yea problems like this are often an indicator that the wrong pattern is being used for the class structure. If it was just he constructor their wouldn't be so much of an issue
– ArtisticPhoenix
Nov 13 '18 at 17:38
plus one purely for It should be obvious
– RiggsFolly
Nov 13 '18 at 17:34
plus one purely for It should be obvious
– RiggsFolly
Nov 13 '18 at 17:34
Yea problems like this are often an indicator that the wrong pattern is being used for the class structure. If it was just he constructor their wouldn't be so much of an issue
– ArtisticPhoenix
Nov 13 '18 at 17:38
Yea problems like this are often an indicator that the wrong pattern is being used for the class structure. If it was just he constructor their wouldn't be so much of an issue
– ArtisticPhoenix
Nov 13 '18 at 17:38
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53286357%2fphp-how-to-solve-method-overloading-in-subclass-declaration-of-should-be-c%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
public static function create($a,$b)
your missing an argument for the child method.– ArtisticPhoenix
Nov 13 '18 at 17:25
Yeah but thats the point. That argument is not needed and
create()
is possible with a single argument, because the subclass "knows" argument $a.– Blackbam
Nov 13 '18 at 17:29
2
Making child classes more restrictive than the classes they extend is a violation of the Liskov substitution principle. Traditional method overriding isn't the pattern you want to use for something like this.
– iainn
Nov 13 '18 at 17:30
@iainn Thanks thats good to know.
– Blackbam
Nov 13 '18 at 17:34