[英]dynamic parameters in abstract methods in php
If I have a class, 如果我上课,
abstract class Parent {
abstract function foo();
}
class Child extends Parent {
function foo($param) {
//stuff
}
}
I get an error because the abstract declaration doesn't have any parameters but the child's implementation of it does. 我得到一个错误,因为抽象声明没有任何参数,但孩子的实现确实如此。 I am making an adapter parent class with abstract functions that, when implemented, could have a variable amount of parameters depending on the context of the child class. 我正在使用抽象函数创建一个适配器父类,这些抽象函数在实现时可能具有可变数量的参数,具体取决于子类的上下文。 Is there any structured way I can overcome this, or do I have to use func_get_args? 有没有任何结构化的方法可以解决这个问题,还是我必须使用func_get_args?
PHP does this to protect polymorphism. PHP这样做是为了保护多态性。 Any object of the inherited type should be usable as if they were of the parent type. 继承类型的任何对象都应该可以使用,就像它们是父类型一样。
Consider the following classes: 考虑以下类:
abstract class Animal {
abstract function run();
}
class Chicken extends Animal {
function run() {
// Clever code that makes a chicken run
}
}
class Horse extends Animal {
function run($speed) {
// Clever code that makes a horse run at a specific speed
}
}
... and the following code: ......以及以下代码:
function makeAnimalRun($animal) {
$animal->run();
}
$someChicken = new Chicken();
$someHorse = new Horse();
makeAnimalRun($someChicken); // Works fine
makeAnimalRun($someHorse); // Will fail because Horse->run() requires a $speed
makeAnimalRun
should be able to execute run
on any instance of Animal
's inherited classes, but since Horse
's implementation of run
requires a $speed
parameter, the $animal->run()
call in makeAnimalRun
fails. makeAnimalRun
应该能够在Animal
的继承类的任何实例上执行run
,但由于Horse
的run
实现需要$speed
参数, makeAnimalRun
的$animal->run()
makeAnimalRun
$animal->run()
调用失败。
Fortunately, there's an easy fix to this. 幸运的是,有一个简单的解决方案。 You just need to provide a default value to the parameter in the overridden method. 您只需要在重写方法中为参数提供默认值。
class Horse extends Animal {
function run($speed = 5) {
// Clever code that makes that horse run at a specific speed
}
}
You have to use func_get_args if you want to have variable arguments in your function. 如果要在函数中使用变量参数,则必须使用func_get_args。 Note that func_get_args gets all the arguments passed to a PHP function. 请注意,func_get_args获取传递给PHP函数的所有参数。
You can however enforce a minimum number of arguments to be passed to the function by including corresponding parameters to them. 但是,您可以通过将相应的参数包含在函数中来强制传递最少数量的参数。
For example: Say that you have a function that you wish to call with at least 1 argument. 例如:假设您有一个希望使用至少1个参数调用的函数。 Then just write the following: 然后写下以下内容:
function foo($param) {
//stuff
$arg_list = func_get_args();
}
Now that you have this definition of foo(), you have to at least call it with one argument. 现在您已经定义了foo(),您必须至少使用一个参数调用它。 You can also choose to pass a variable number of arguments n where n > 1, and receive those arguments through func_get_args(). 您还可以选择传递可变数量的参数n,其中n> 1,并通过func_get_args()接收这些参数。 Keep in mind that $arg_list above will also contain a copy of $param as its first element. 请记住,上面的$ arg_list还将包含$ param的副本作为其第一个元素。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.