I have some legacy code and refactored an array to an ArrayObject. Now I have problems checking, if a variable is an array:
我有一些遗留代码并将数组重构为ArrayObject。现在我有问题检查,如果变量是一个数组:
if (is_array($entity) && $otherCondition) {
// do something
}
The function is_array()
returns false
on an ArrayObject. See this report.
函数is_array()在ArrayObject上返回false。见这份报告。
Simplest solution would be to use something like this:
最简单的解决方案是使用这样的东西:
function is_traversable($var) {
return is_array($var) || $var instanceof Traversable;
}
Is there some native way for PHP to do a check like this?
是否有一些本地方式让PHP进行这样的检查?
3 个解决方案
#1
according to http://blog.stuartherbert.com/php/2010/07/19/should-is_array-accept-arrayobject/, you have to make the custom method you wrote in order to do what you wish...
根据http://blog.stuartherbert.com/php/2010/07/19/should-is_array-accept-arrayobject/,您必须制作自己编写的自定义方法,以便按照您的意愿行事......
#2
ArrayObject does contain a method named getArrayCopy() which allow you to get an array copy of your ArrayObject. I suppose that most of array built-in functions can be applied on this copy ;)
ArrayObject确实包含一个名为getArrayCopy()的方法,它允许您获取ArrayObject的数组副本。我想大多数数组内置函数都可以应用于此副本;)
Doc : http://php.net/manual/fr/arrayobject.getarraycopy.php
Doc:http://php.net/manual/fr/arrayobject.getarraycopy.php
#3
NO
is the answer to your question. An array is an array and an object is an object.
是你的问题的答案。数组是数组,对象是对象。
So, if you change an array to an object you should change all the checks. I don't like your is_traversable($var)
function because it means you only do half a job of refactoring you code. You should replace is_array($entity)
by $entity instanceof myEntityClass
or is_object($entity)
.
因此,如果将数组更改为对象,则应更改所有检查。我不喜欢你的is_traversable($ var)函数,因为这意味着你只需要完成一半的重构代码。您应该用$ entity instanceof myEntityClass或is_object($ entity)替换is_array($ entity)。
In any case you should not see arrays
as old fashioned. It can very well be that an array should become an object, but there's nothing wrong with arrays as such.
在任何情况下,您都不应该将阵列看作是老式的。很可能数组应该成为一个对象,但数组本身没有任何问题。
#1
according to http://blog.stuartherbert.com/php/2010/07/19/should-is_array-accept-arrayobject/, you have to make the custom method you wrote in order to do what you wish...
根据http://blog.stuartherbert.com/php/2010/07/19/should-is_array-accept-arrayobject/,您必须制作自己编写的自定义方法,以便按照您的意愿行事......
#2
ArrayObject does contain a method named getArrayCopy() which allow you to get an array copy of your ArrayObject. I suppose that most of array built-in functions can be applied on this copy ;)
ArrayObject确实包含一个名为getArrayCopy()的方法,它允许您获取ArrayObject的数组副本。我想大多数数组内置函数都可以应用于此副本;)
Doc : http://php.net/manual/fr/arrayobject.getarraycopy.php
Doc:http://php.net/manual/fr/arrayobject.getarraycopy.php
#3
NO
is the answer to your question. An array is an array and an object is an object.
是你的问题的答案。数组是数组,对象是对象。
So, if you change an array to an object you should change all the checks. I don't like your is_traversable($var)
function because it means you only do half a job of refactoring you code. You should replace is_array($entity)
by $entity instanceof myEntityClass
or is_object($entity)
.
因此,如果将数组更改为对象,则应更改所有检查。我不喜欢你的is_traversable($ var)函数,因为这意味着你只需要完成一半的重构代码。您应该用$ entity instanceof myEntityClass或is_object($ entity)替换is_array($ entity)。
In any case you should not see arrays
as old fashioned. It can very well be that an array should become an object, but there's nothing wrong with arrays as such.
在任何情况下,您都不应该将阵列看作是老式的。很可能数组应该成为一个对象,但数组本身没有任何问题。