代码:
<?php class MyRedis extends \Redis { public function set($key,$value,$timeout = null) { parent::set($key, $value, $timeout); } } $r1 = new MyRedis; $r1->connect('192.168.0.222', 6379); $r1->set(1,2); echo PHP_EOL, '-----', PHP_EOL, PHP_EOL; class MyRedis2 extends \Redis { public function set($key, $value, $timeout = null, $opt = null) { parent::set($key, $value, $timeout, $opt); } } $r2 = new MyRedis2; $r2->connect('192.168.0.222', 6379); $r2->set(1,2);
结果:
PHP Warning: Declaration of MyRedis::set($key, $value, $timeout = NULL) should be compatible with Redis::set($key, $value, $timeout = NULL, $opt = NULL) in /mnt/d/projects/xxx/test.php on line 8
-----
PHP Warning: Redis::set() expects at most 3 parameters, 4 given in /mnt/d/projects/xxx/test.php on line 19
第一个类,定义3个参数,说要求定义4个参数。
第二个类,我定义4个参数,也传了4个参数给父类方法,结果……
哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈……