开发者

array_slice (or other array_* functions) on ArrayObject

开发者 https://www.devze.com 2023-03-18 02:50 出处:网络
I have a question regarding ArrayObject. I wanted to use array_sl开发者_Go百科ice in an ArrayObject class and I couldn\'t. Is there a way to do it, without needing to write an \"slice\" method to the

I have a question regarding ArrayObject. I wanted to use array_sl开发者_Go百科ice in an ArrayObject class and I couldn't. Is there a way to do it, without needing to write an "slice" method to the class that implements ArrayObject?


You can always work on the array copy:

$array = $object->getArrayCopy();
// modify $array as needed, e.g. array_slice(....) 
$object = new ArrayObject($array);

There sometime in the past was the idea to make all functions that accept arrays (or probably many of them) to accept ArrayObject as well. But I dunno how far that has gone and if it's still followed. I think ArrayObject is more a behavioural thing than actually replacing the native array in PHP.

Related question: PHP Array and ArrayObject


Having a class that wraps php array functions is not that bad idea. Will make code much cleaner.

echo $myAry->slice(10, 5)->reverse()->join(", ");

Just like a normal language, you know.

0

精彩评论

暂无评论...
验证码 换一张
取 消