php数组初始化

我需要在PHP中初始化一个对象数组。
目前我有以下代码:</ p> \ n

  $ comment = array(); 
</ code> </ pre>

当我向数组添加元素时</ p>
\ n

  public function addComment($ c){
array_push($ this-&gt; comment,$ c);
}
</ code> </ pre>

这里, $ c </ code>是类Comment的对象。</ p>

但是当我尝试使用 $ comment </ code>访问该类的函数时 ,我得到以下错误:</ p>


致命错误:在非对象上调用成员函数
getCommentString()</ p>
</ blockquote >

谁能告诉我如何在php中初始化一个对象数组?</ p>

谢谢
Sharmi </ p>
</ div>

展开原文

原文

I need to initialize an array of objects in PHP. Presently I have the following code:

$comment = array();

And when i am adding an element to the array

public function addComment($c){
    array_push($this->comment,$c);
}

Here, $c is an object of class Comment.

But when I try to access an functions of that class using $comment, I get the following error:

Fatal error: Call to a member function getCommentString() on a non-object

Can anyone tell me how to initialize an array of objects in php?

Thanks Sharmi

douxi3233
douxi3233 您无法通过调用$comment来访问变量$this->comment。
接近 11 年之前 回复

4个回答



对我来说看起来像是一个范围问题。</ p>

如果 $ comments </ code >是类的成员,在该类的函数内调用 $ comments </ code>实际上不会使用该成员,而是使用属于该范围的 $ comments </ code>的实例 函数。</ p>

如果您想尝试使用类成员,请执行 $ this-&gt; comments </ code>,而不仅仅是 $ 注释</ code>。</ p>

  class foo 
{
private $ bar;

function add_to_bar($ param)
{
//添加到 一个$ bar,它只存在于这个
// add_to_bar()函数中。
$ bar [] = $ param;

//添加到属于
//类的$ bar变量,而不是 只是add_to_bar()函数。
$ this-&gt; bar [] = $ param;
}
}
</ code> </ pre>
</ div>

展开原文

原文

Looks like a scope problem to me.

If $comments is a member of a class, calling $comments inside a function of that class will not actually use the member, but rather use an instance of $comments belonging to the scope of the function.

If other words, if you are trying to use a class member, do $this->comments, not just $comments.

class foo
{
    private $bar;

    function add_to_bar($param)
    {
        // Adds to a $bar that exists solely inside this
        // add_to_bar() function.
        $bar[] = $param;

        // Adds to a $bar variable that belongs to the
        // class, not just the add_to_bar() function.
        $this->bar[] = $param;
    }
}



  $ this-&gt; comment = array(); 
</ code> </ pre>
</ DIV>

展开原文

原文

$this->comment = array();

duangong0690
duangong0690 如果初始化发生在构造函数之外,则不会
接近 7 年之前 回复



此代码可能对您有所帮助:</ p>

  $ comments = array(); \  n $ comments [] = new ObjectName();  //将第一个对象添加到数组
$ comments [] = new ObjectName(); //将第二个对象添加到数组

//要访问对象,需要使用数组的索引
//所以你可以这样做:
echo $ comments [0] - &gt; getCommentString() ; //第一个对象
echo $ comments [1] - &gt; getCommentString(); //第二个对象

//或循环它们
foreach($ comments as $ comment){
echo $ comment-&gt; getCommentString();
}
</ code> </ pre> \ n

我认为您的问题是您是如何将对象添加到数组中的($ this-&gt;注释引用的是什么?)或者您可能尝试在数组上调用 - &gt; getCommentString() 而不是数组中的实际对象。</ p>
</ div>

展开原文

原文

This code might help you:

$comments = array();
$comments[] = new ObjectName(); // adds first object to the array
$comments[] = new ObjectName(); // adds second object to the array

// To access the objects you need to use the index of the array
// So you can do this:
echo $comments[0]->getCommentString(); // first object
echo $comments[1]->getCommentString(); // second object

// or loop through them
foreach ($comments as $comment) {
    echo $comment->getCommentString();
}

I think your problem is either how you are adding the objects to the array (what is $this->comment referencing to?) or you may be trying to call ->getCommentString() on the array and not on the actual objects in the array.



您可以通过将数组传递给 print_r()</ code> :</ p>

  print_r($ comment);

</ code> </ pre>

假设你有 Comment </ code>对象,你应该可以用 $ comment [0] - &gt引用它们 ; getCommentString()</ code>。</ p>
</ div>

展开原文

原文

You can see what's in the array by passing it to print_r():

print_r($comment);

Presuming you have Comment objects in there, you should be able to reference them with $comment[0]->getCommentString().

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐