这个警告与现实世界无关。忘记学术例子,看看现实世界的例子。有 1000 家 LOCK 制造商制造带有组合触摸板和钥匙覆盖的门锁。他们是否使用 2 个锁舌 - 一个用于组合锁,一个用于钥匙锁?不。门闩锁被抽象为钥匙锁和密码锁。那是真实的世界。PHP(谁会很快拒绝任何关于这个主题的错误报告)想在这个问题上坚持到底。
class abstractLock
{
private $locked = true;
public function unlock()
{
$this->locked=false;
}
public function lockStatus()
{
if($this->locked) return "Locked\n";
else return "Unlocked\n";
}
}
class combinationLock extends abstractLock
{
private $combination = '32-10-21'; // combination
public function unlock($combination) // unlock WITH combination
{
if($this->combination == $combination) parent::unlock();
}
}
class paddleLock extends abstractLock
{
private $key = '100,70,80,30,50,90,60,40,100'; // ridge heights
public function unlock($key) // unlock WITH key
{
if($this->key == $key) parent::unlock();
}
}
$lock1 = new paddleLock();
echo "paddleLock is " . $lock1->lockStatus();
$lock1->unlock('100,70,80,30,50,90,60,40,100');
echo "paddleLock is " . $lock1->lockStatus();
$lock2 = new combinationLock();
echo "combinationLock is " . $lock2->lockStatus();
$lock2->unlock('32-10-21');
echo "combinationLock is " . $lock2->lockStatus();
PHP 甚至不愿意讨论这个问题。所以我所有的代码都将包含以下内容来消除这个 PHP错误:
// FIX PHP's bogus warning of: PHP Warning: Declaration of * should be compatible with
if (PHP_MAJOR_VERSION >= 7) {
set_error_handler(function ($errno, $errstr) {
return strpos($errstr, 'Declaration of') === 0;
}, E_WARNING);
}