-1

我有以下 PHP 代码:

class SQLStatements {
    public function __construct($iName) {
        $this->name = $iName;
        $this->sqlStatement = array ();
    }

    public function addStatement($iSQLStatement) {
        $this->sqlStatement [] = $iSQLStatement;
    }

...other functions...
}

print_r ($ss);
$sqlStatements->addStatement ( $ss );   
print_r ($sqlStatements);

输出是:

SQL语句对象
(
    [id] => 300
    [时间戳] => 201301090222
    [夹具] => 主场 v 客场
    [结果] => 主页
    [fixtureDateTime] => 2013-01-11
    [bookmakerOdds] => 数组
        (
            [0] => 2.15
            [1] => 2.22
            [2] => 2.05
            [3] => 2.05
            [4] => 2.15
            [5] => 2.20
            [6] => 2.14
        )

)
SQLStatements 对象
(
    [名称] => 声明
    [sqlStatement] => 数组
        (
            [0] => SQLStatement 对象
                (
                    [id] => 300
                    [时间戳] => 201301090222
                    [夹具] => 主场 v 客场
                    [结果] => 主页
                    [fixtureDateTime] => 2013-01-11
                    [bookmakerOdds] => 数组
                        (
                            [0] => 2.15
                            [1] => 2.22
                            [2] => 2.05
                            [3] => 2.05
                            [4] => 2.15
                            [5] => 2.20
                            [6] => 2.14
                        )

                )

        )

)
SQL语句对象
(
    [id] => 300
    [时间戳] => 201301090222
    [夹具] => 主场 v 客场
    [结果] => 主场 v 客场 - 平局
    [fixtureDateTime] => 2013-01-11
    [bookmakerOdds] => 数组
        (
            [0] => 3.50
            [1] => 3.50
            [2] => 3.40
            [3] => 3.40
            [4] => 3.30
            [5] => 3.50
            [6] => 3.28
        )

)
SQLStatements 对象
(
    [名称] => 声明
    [sqlStatement] => 数组
        (
            [0] => SQLStatement 对象
                (
                    [id] => 300
                    [时间戳] => 201301090222
                    [夹具] => 主场 v 客场
                    [结果] => 主场 v 客场 - 平局
                    [fixtureDateTime] => 2013-01-11
                    [bookmakerOdds] => 数组
                        (
                            [0] => 3.50
                            [1] => 3.50
                            [2] => 3.40
                            [3] => 3.40
                            [4] => 3.30
                            [5] => 3.50
                            [6] => 3.28
                        )

                )

            [1] => SQLStatement 对象
                (
                    [id] => 300
                    [时间戳] => 201301090222
                    [夹具] => 主场 v 客场
                    [结果] => 主场 v 客场 - 平局
                    [fixtureDateTime] => 2013-01-11
                    [bookmakerOdds] => 数组
                        (
                            [0] => 3.50
                            [1] => 3.50
                            [2] => 3.40
                            [3] => 3.40
                            [4] => 3.30
                            [5] => 3.50
                            [6] => 3.28
                        )

                )

        )

)

$ss是一个SQLStatement对象,并且该SQLStatements对象是对象的集合SQLStatement。但是,从输出中可以看出,SQLStatements->SQLStatement[0] 和 SQLStatements->SQLStatement[1] 是相同的。我期待 SQLStatements->SQLStatement[0]->结果是 'Home',而不是 'Home v Away - Draw'。我究竟做错了什么?

4

1 回答 1

1

当一个对象被传递给 PHP 中的一个方法时,PHP 实际上是在内存中传递该对象地址的值。这意味着,当您调用 时addStatement($ss),您不会将$ss对象的副本添加到 statements 数组 - 您正在添加指向内存中该对象的指针。如果您稍后更改该$ss对象,它也会在您的 SQLStatements 类中的数组上发生更改(因为它们指向内存中的同一个对象)。

您应该做的是,在调用之后addStatement($ss)您应该执行以下操作:$ss = new SQLStatement()然后您可以进行更改并再次添加它(通过创建一个您已经为这个新对象分配了另一个内存块,并且该位置有一个新内存不会与分配给的旧对象冲突的地址。)new SQLStatement()$ss

于 2013-01-08T17:11:38.350 回答