PHP 无法打印对象数组


php fail to print an array of object

我的php代码是:

<?php
    class Product {
        var $product_name;
        var $retailer;
        function __constructor($product, $retailer) {
            $this->product_name = $product;
            $this->retailer = $retailer;
        }
        function getProduct() {
            return $this->product_name;
        }
    }
    $product_arr = array();
    for ($f = 0; $f < 100; $f++) {
        array_push($product_arr, new Product("asd", "xcxcxc"));
    }
    print_r($product_arr);
?>

代码非常简单,我有一个名为"Product"的类,我构建了一个由 100 个 Product 对象组成的数组,但是当我尝试打印数组时,我发现所有对象的product_name和零售商字段都是空的。不知道为什么会这样。

错误的名称:

    function __constructor($product, $retailer) {
                       ^^^

PHP 的标准构造函数名称只是 __construct(没有or)。因此,您从未实际调用构造函数,这意味着您的变量赋值从未执行过。