PHP 对象数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8612190/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:05:31  来源:igfitidea点击:

Array of PHP Objects

phparraysobject

提问by Beamer180

So I have been searching for a while and cannot find the answer to a simple question. Is it possible to have an array of objects in PHP? Such as:

所以我一直在寻找一段时间,但找不到一个简单问题的答案。是否可以在 PHP 中拥有一组对象?如:

$ar=array();    
$ar[]=$Obj1    
$ar[]=$obj2

For some reason I have not been able to find the answer anywhere. I assume it is possible but I just need to make sure.

出于某种原因,我无法在任何地方找到答案。我认为这是可能的,但我只需要确保。

回答by Mike Purcell

The best place to find answers to general (and somewhat easy questions) such as this is to read up on PHP docs. Specifically in your case you can read more on objects. You can store stdObject and instantiated objects within an array. In fact, there is a process known as 'hydration' which populates the member variables of an object with values from a database row, then the object is stored in an array (possibly with other objects) and returned to the calling code for access.

找到诸如此类的一般性(以及有些简单的问题)答案的最佳位置是阅读PHP 文档。特别是在您的情况下,您可以阅读有关对象的更多信息。您可以在数组中存储 stdObject 和实例化对象。事实上,有一个称为“ hydration”的过程,它用数据库行中的值填充对象的成员变量,然后将该对象存储在一个数组中(可能与其他对象一起)并返回给调用代码进行访问。

-- Edit --

- 编辑 -

class Car
{
    public $color;
    public $type;
}

$myCar = new Car();
$myCar->color = 'red';
$myCar->type = 'sedan';

$yourCar = new Car();
$yourCar->color = 'blue';
$yourCar->type = 'suv';

$cars = array($myCar, $yourCar);

foreach ($cars as $car) {
    echo 'This car is a ' . $car->color . ' ' . $car->type . "\n";
}

回答by ceejayoz

Yes.

是的。

$array[] = new stdClass;
$array[] = new stdClass;

print_r($array);

Results in:

结果是:

Array
(
    [0] => stdClass Object
        (
        )

    [1] => stdClass Object
        (
        )

)

回答by Langel

Arrays can hold pointers so when I want an array of objects I do that.

数组可以保存指针,所以当我想要一个对象数组时,我会这样做。

$a = array();
$o = new Whatever_Class();
$a[] = &$o;
print_r($a);

This will show that the object is referenced and accessible through the array.

这将表明该对象已通过数组被引用和访问。

回答by rizon

Yes, its possible to have array of objects in PHP.

是的,在 PHP 中可以有对象数组。

class MyObject {
  private $property;

  public function  __construct($property) {
    $this->Property = $property;
  }
}
$ListOfObjects[] = new myObject(1); 
$ListOfObjects[] = new myObject(2); 
$ListOfObjects[] = new myObject(3); 
$ListOfObjects[] = new myObject(4); 

print "<pre>";
print_r($ListOfObjects);
print "</pre>";

回答by Fibo

Although all the answers given are correct, in fact they do not completely answer the question which was about using the [] construct and more generally filling the array with objects.

尽管给出的所有答案都是正确的,但实际上它们并没有完全回答关于使用 [] 构造以及更一般地用对象填充数组的问题。

A more relevant answer can be found in how to build arrays of objects in PHP without specifying an index number?which clearly shows how to solve the problem.

可以在如何在不指定索引号的情况下在 PHP 中构建对象数组中找到更相关的答案 这清楚地说明了如何解决问题。