PHP 中的类 Java 集合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1580223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java-like Collections in PHP
提问by ivmos
I'm learning PHP5 (last time I checked PHP was in PHP4 days) and I'm glad to see that PHP5 OO is more Java-alike than the PHP4 one but there's still an issue that makes me feel quite unconfortable because of my Java background : ARRAYS.
我正在学习 PHP5(上次我检查 PHP 是在 PHP4 天),我很高兴看到 PHP5 OO 比 PHP4 更像 Java,但仍然有一个问题让我感觉很不舒服,因为我的 Java背景:阵列。
I'm reading "Proffesional PHP6" (Wrox) and It shows its own Collection implementation. I've found other clases like the one in http://aheimlich.dreamhosters.com/generic-collections/Collection.phpsbased on SPL. I've also found that there's some kind of Collection in SPL (ArrayObject)
我正在阅读“Proffesional PHP6”(Wrox)并且它展示了它自己的 Collection 实现。我发现了其他类,如基于 SPL 的http://aheimlich.dreamhosters.com/generic-collections/Collection.phps 中的类。我还发现 SPL (ArrayObject) 中有某种集合
However, I'm surprised because I don't really see people using Collections in PHP, they seem to prefer arrays.
然而,我很惊讶,因为我并没有真正看到人们在 PHP 中使用集合,他们似乎更喜欢数组。
So, isn't it a good idea using Collections in PHP just like people use ArrayList instead of basic arrays in Java? After all, php arrays aren't really like java arrays.
那么,在 PHP 中使用 Collections 难道不是一个好主意,就像人们在 Java 中使用 ArrayList 而不是基本数组一样吗?毕竟,php 数组并不像 java 数组。
回答by Pras
Collections in Java make a lot of sense since it's a strongly typed language. It makes sense to have a collection of say "Cars" and another of "Motorbikes".
Java 中的集合很有意义,因为它是一种强类型语言。有一个说“汽车”和另一个“摩托车”的集合是有意义的。
However, in PHP, due to the dynamically typed nature, it is quite common to sacrifice the formality of Collections. Arrays are sufficient to be used as generic containers of various object types (Cars, Motorbikes, etc.). Also, the added benefit comes from the fact that arrays can be mutated very easily (which sometimes can be a big disadvantage when proper error checking is absent).
然而,在 PHP 中,由于动态类型的特性,牺牲集合的形式是很常见的。数组足以用作各种对象类型(汽车、摩托车等)的通用容器。此外,额外的好处来自这样一个事实,即数组可以很容易地改变(当没有正确的错误检查时,这有时可能是一个很大的缺点)。
I come from a Java background, and I've found that using a Collections design pattern in PHP does not buy much in the way of advantages (no multi-threading, no optimization of memory allocation, no iterators, etc.).
我来自 Java 背景,我发现在 PHP 中使用 Collections 设计模式并没有带来太多优势(没有多线程、没有优化内存分配、没有迭代器等)。
If you're looking for any of those advantages, its probably better to construct a wrapper class around the array, implementing each feature (iterators, etc.) a la carte.
如果您正在寻找这些优势中的任何一个,那么围绕数组构建一个包装类可能会更好,实现每个功能(迭代器等)点菜。
回答by Gavin
I am very pro collection objects in PHP, they can be used to add type safety, impliment easy to use search, sort and manipulation functionality, and represent the correct OO approach rather then using arrays and the multitude of useful but procedual functions that operate on them in differing patterns all over the source.
我非常喜欢 PHP 中的集合对象,它们可用于添加类型安全,实现易于使用的搜索、排序和操作功能,并表示正确的 OO 方法,而不是使用数组和大量有用但程序化的函数。它们以不同的模式遍布整个源头。
We have various collections that we use for various purposes all neatly inherited promoting type safety, consistent coding standards and a high level of code reuse.
我们有各种用于各种目的的集合,所有这些集合都是整齐地继承的,以促进类型安全、一致的编码标准和高水平的代码重用。
But ultimatley, they are all array's internally!
但归根结底,它们都是阵列内部的!
I suppose really it comes down to choice, but in my object oriented world I like to keep easily repeatable segments of code such as sort and search algorithms in base classes, and I find the object notation more self documenting.
我想这真的归结为选择,但在我的面向对象世界中,我喜欢在基类中保留易于重复的代码段,例如排序和搜索算法,并且我发现对象符号更能自我记录。
回答by Nick Veys
PHP arrays are associative... They're far more powerful than Java's arrays, and include much of the functionality of List<> and Map<>.
PHP 数组是关联的……它们比 Java 的数组强大得多,并且包含 List<> 和 Map<> 的大部分功能。
What do you mean by "good idea"? They're different tools, using one language in the way you used another usually results in frustration.
你所说的“好主意”是什么意思?它们是不同的工具,以您使用另一种语言的方式使用一种语言通常会导致沮丧。
回答by Dan Shafer
I, too, was somewhat dismayed to find no Collection type classes in PHP. Arrays have a couple of real disadvantages in my experience.
我也有点沮丧地发现 PHP 中没有 Collection 类型的类。根据我的经验,数组有几个真正的缺点。
First, the number of functions available to manipulate them is somewhat limited. For example, I need to be able to arbitrarily insert and remove items to/from a Collection at a given index position. Doing that with the built-in language functions for arrays in PHP is painful at best.
首先,可用于操作它们的函数数量有限。例如,我需要能够在给定索引位置的集合中任意插入和删除项目。使用 PHP 中数组的内置语言函数来做到这一点充其量是痛苦的。
Second, as a sort of offshoot of the first point, writing clean, readable code that manipulates arrays at any level of complexity beyond simple push/pop and iterator stuff is difficult at best. I often find that I have to use one array to index and keep track of another array in data-intensive apps I create.
其次,作为第一点的一种分支,编写干净、可读的代码来操作除简单的 push/pop 和迭代器之外的任何复杂级别的数组,充其量是困难的。我经常发现我必须使用一个数组来索引和跟踪我创建的数据密集型应用程序中的另一个数组。
I prefer working in a framework (my personal choice is NOLOH). There, I have a real Collection class called ArrayList that has functions such as Add, Insert, RemoveAt, RemoveRange and Toggle. I imagine other PHP frameworks address this issue as well.
我更喜欢在框架中工作(我个人的选择是NOLOH)。在那里,我有一个名为 ArrayList 的真正 Collection 类,它具有诸如 Add、Insert、RemoveAt、RemoveRange 和 Toggle 之类的函数。我想其他 PHP 框架也会解决这个问题。
回答by WonderLand
A nice implementation of collection in php is provided by Varien Lib, this library is part of Magento code with OSL license. ( more info about Magento license and code reuse here.
Varien Lib 提供了一个很好的 php 集合实现,该库是具有 OSL 许可的 Magento 代码的一部分。(有关 Magento 许可证和代码重用的更多信息请点击此处。
Cannot find any source code for the library so the best way is to download magento and then look in /lib/Varien/
找不到该库的任何源代码,因此最好的方法是下载 magento,然后查看 /lib/Varien/
回答by sumit
Yii has implementation of full java like collections stack
Yii 实现了像集合堆栈这样的完整 Java
回答by Gaz_Edge
I sometimes use this really simple implementation to give me a rough and ready collection.
我有时会使用这个非常简单的实现来给我一个粗略和准备好的集合。
Normally the main requirement of a collection is enforcing a group of one type of object, you just have to setup a basic class with a constructor to implement it.
通常,集合的主要要求是强制执行一组一种类型的对象,您只需要设置一个带有构造函数的基本类来实现它。
class SomeObjectCollection {
/**
* @var SomeObject[]
*/
private $collection = array();
/**
* @param SomeObject $object1
* @param SomeObject $_ [optional]
*/
function __construct(SomeObject $object1 = null, SomeObject $_ = null)
{
foreach (func_get_args() as $index => $arg) {
if(! $arg instanceof SomeObject) throw new \RuntimeException('All arguments must be of type SomeObject');
$this->collection[] = $arg;
}
}
/**
* @return SomeObject[]
*/
public function getAll()
{
return $this->collection;
}
}

