让我们说我有这些课程:
abstract class Car
{
abstract public function handle(array $data);
}
class CarA extends Car
{
public function handle(array $data)
{
echo $data['color'];
}
}
$car= new CarA();
// Here I can pass args as an array
$car->handle([
"color" => "white"
]);
哪个工作正常。
现在我不想传递任何参数
class CarB extends Car
{
public function handle()
{
echo 'CarB';
}
}
$car= new CarB();
// Here I dont wnat to pass any args, I just want to call the method.
$car->handle();
这不起作用。我遇到错误:must be compatible with
我想要什么?
我希望“句柄”功能仅在传递数组数据或不传递数组数据时起作用。。
答案 0 :(得分:1)
如@ jacki360所述,最接近的方法是使用func_get_args()。
SELECT consecutive_id, MIN(id) as id
FROM (
SELECT a.consecutive_id, MIN(id) AS id
FROM test AS a
JOIN (
SELECT consecutive_id
FROM test
GROUP BY consecutive_id
HAVING COUNT(DISTINCT unique_id) > 1) AS b
ON a.consecutive_id = b.consecutive_id
GROUP BY a.consecutive_id, a.unique_id
HAVING COUNT(*) = 1) AS x
GROUP BY consecutive_id
答案 1 :(得分:0)
您的函数必须具有相同的签名,因此您可以尝试的是使该函数不带参数:handle() 当您需要参数时,只需在调用方法时将其传递并使用 func_get_args
答案 2 :(得分:0)
abstract class Car {
abstract public function handle();
public $data;
public $color;
public function setArguments(Array $arguments, $recursive = false) {
$this->data = $recursive === true ? $this->data : array();
// Do additional formatting here if needed
foreach ($arguments as $key => $argument) {
if (is_array($argument)) {
$this->setArguments($argument, true);
} else {
$this->data[$key] = $argument;
}
}
$this->setColor();
}
public function setColor() {
$this->color = !empty($this->data['color']) ? $this->data['color'] : 'Any';
}
}
class CarA extends Car {
public function handle() {
$this->setArguments(func_get_args());
echo 'Color: ' . $this->color;
}
}
class CarB extends Car {
public function handle() {
$this->setArguments(func_get_args());
echo 'Color: ' . $this->color;
}
};
$carA = new CarA();
$carA->handle(array(
'color' => 'Color1'
));
$carB = new CarB();
$carB->handle();
$carB->handle(array(
'color' => 'Color2'
));