PHP数组对象集合问题

时间:2013-01-08 17:08:53

标签: php

我有以下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);

输出是:

SQLStatement Object
(
    [id] => 300
    [timestamp] => 201301090222
    [fixture] => Home v Away
    [outcome] => Home
    [fixtureDateTime] => 2013-01-11
    [bookmakerOdds] => Array
        (
            [0] => 2.15
            [1] => 2.22
            [2] => 2.05
            [3] => 2.05
            [4] => 2.15
            [5] => 2.20
            [6] => 2.14
        )

)
SQLStatements Object
(
    [name] => TheStatement
    [sqlStatement] => Array
        (
            [0] => SQLStatement Object
                (
                    [id] => 300
                    [timestamp] => 201301090222
                    [fixture] => Home v Away
                    [outcome] => Home
                    [fixtureDateTime] => 2013-01-11
                    [bookmakerOdds] => Array
                        (
                            [0] => 2.15
                            [1] => 2.22
                            [2] => 2.05
                            [3] => 2.05
                            [4] => 2.15
                            [5] => 2.20
                            [6] => 2.14
                        )

                )

        )

)
SQLStatement Object
(
    [id] => 300
    [timestamp] => 201301090222
    [fixture] => Home v Away
    [outcome] => Home v Away - Draw
    [fixtureDateTime] => 2013-01-11
    [bookmakerOdds] => Array
        (
            [0] => 3.50
            [1] => 3.50
            [2] => 3.40
            [3] => 3.40
            [4] => 3.30
            [5] => 3.50
            [6] => 3.28
        )

)
SQLStatements Object
(
    [name] => TheStatement
    [sqlStatement] => Array
        (
            [0] => SQLStatement Object
                (
                    [id] => 300
                    [timestamp] => 201301090222
                    [fixture] => Home v Away
                    [outcome] => Home v Away - Draw
                    [fixtureDateTime] => 2013-01-11
                    [bookmakerOdds] => Array
                        (
                            [0] => 3.50
                            [1] => 3.50
                            [2] => 3.40
                            [3] => 3.40
                            [4] => 3.30
                            [5] => 3.50
                            [6] => 3.28
                        )

                )

            [1] => SQLStatement Object
                (
                    [id] => 300
                    [timestamp] => 201301090222
                    [fixture] => Home v Away
                    [outcome] => Home v Away - Draw
                    [fixtureDateTime] => 2013-01-11
                    [bookmakerOdds] => Array
                        (
                            [0] => 3.50
                            [1] => 3.50
                            [2] => 3.40
                            [3] => 3.40
                            [4] => 3.30
                            [5] => 3.50
                            [6] => 3.28
                        )

                )

        )

)

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

1 个答案:

答案 0 :(得分:1)

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

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