允许 PHP 函数变量是字符串或数组是模式还是反模式?
来自C#,我习惯于使用可变类型参数重载我的方法。由于您无法在 PHP 中执行此操作,因此我经常创建如下例所示的方法,该方法接受变量,然后我检查类型并采取相应措施:
showLength('one');
showLength(array(
'one',
'two',
'three'
));
function showLength($stringOrArray) {
$arr = array();
if(is_array($stringOrArray)) {
$arr = $stringOrArray;
} else if(is_string($stringOrArray)) {
$arr[] = $stringOrArray;
} else {
//exception
}
foreach ($arr as $str) {
echo strlen($str).'<br/>';
}
}
输出:
3
3
3
5
4
This给了我与 C# 相同的功能,但看起来有点混乱,好像有更好的方法。
这是在 PHP (5.3) 中进行方法重载的公认方法还是有更好的方法?
Coming from C# I'm used to overloading my methods with variably typed parameters. Since you can't do this in PHP, I often create methods such as the example below which accept a variable, then I check the type and act accordingly:
showLength('one');
showLength(array(
'one',
'two',
'three'
));
function showLength($stringOrArray) {
$arr = array();
if(is_array($stringOrArray)) {
$arr = $stringOrArray;
} else if(is_string($stringOrArray)) {
$arr[] = $stringOrArray;
} else {
//exception
}
foreach ($arr as $str) {
echo strlen($str).'<br/>';
}
}
output:
3
3
3
5
4
This gives me the same functionality as in C# but it seems a bit messy, as if there is a better way.
Is this the accepted way to do method overloading in PHP (5.3) or is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我知道很多框架都会对某些有意义的函数执行此操作,PHP 的一些核心函数也是如此。对我来说,“好”用途和“差”用途之间的区别在于文档(docblock)。文档需要表明参数的类型是混合的以及不同的可接受的变量类型。
例如:
I know a lot of frameworks do this for certain functions where it makes sense, as do some of PHP's core functions. The difference between 'good' uses and 'poor' uses to me is the documentation (docblock). The documentation needs to indicate that the param is of type mixed and the different acceptable variable types.
For example:
我不喜欢打败一匹马,但我仍然会在这里权衡。
我不认为这是一种反模式;正如 @Merijn 指出的,许多本机 PHP 函数接受混合类型参数。此外,在您自己的许多情况下,为了简洁起见,函数需要接受给定类型的元素或集合,而不是将其拆分为两个函数。
转换为
(array)
是实现此功能的一种快速且简单的方法:使用
$foo = (array) $foo;
更好1与$foo = array($foo);
相比,当$foo
已经是一个数组时,它不会再次包装它。参考
1 用标量生成所需的结果;物体产生不同的结果。转换为数组的对象将枚举属性,因此请谨慎使用。
I don't like to beat a dead horse, but still, I'll weigh in here.
I wouldn't consider it an anti-pattern; as @Merijn points out, many native PHP functions accept mixed type parameters. Furthermore, many circumstances as your own, require that for succinctness the function accept an element, or a collection, of a given type instead of splitting it over two functions.
Casting to
(array)
is a quick and easy way to achieve this functionality:Using
$foo = (array) $foo;
is better1 than$foo = array($foo);
as when$foo
is already an array, it won't wrap it again.Reference
1 Produces the desired results with scalars; objects produce different results. Objects cast to array will enumerate the properties, so use discretion.
正如您所说,PHP 中确实没有像 C# 那样接受多个类型参数的方法,但是只要您在对参数执行任何操作之前检查参数的类型,就不会造成问题。
许多原生 PHP 函数接受混合类型参数,例如 str_replace() 。
Like you said, there isn't really a method of accepting multiple type parametersin PHP like C# does, but this shouldn't cause problems as long as you check what type the parameters are before doing anything with them.
A lot of native PHP functions accept mixed type parameters, e.g. str_replace().