Skip to content

Instantly share code, notes, and snippets.

@mugyu
Created March 11, 2019 09:42
Show Gist options
  • Save mugyu/dbf297a544446bbcafa0c56ec21cb5a9 to your computer and use it in GitHub Desktop.
Save mugyu/dbf297a544446bbcafa0c56ec21cb5a9 to your computer and use it in GitHub Desktop.
php7: 継承したメソットの引数違いでワーニング
<?php
class A
{
protected function data(int $id)
{
return "A";
}
}
class B extends A
{
protected function data(string $id)
{
return parent::data(1);
}
public function say() {
echo $this->data() . "\n";
}
}
$b = new B();
/*
* Warning: Declaration of B::data(string $id) should be compatible with A::data(int $id)
* PHP Warning: Declaration of B::data(string $id) should be compatible with A::data(int $id)
*
* -------
*
* php7のバージョンから継承したメソッドの引数の型や数が変わるとワーニングになるらしい。
* 親と子で引数の数が変わるぐらいよくある事だと思ってたけどそうでもないのか?
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment