hidden
hidden

Reputation: 3236

An Array of objects in php

I dont understand this why i get this error! is so simple yet very different in php.

<?php
    $x=new DisplayTaxonomy ();
    $myarray= array();
    $x->setADTitle("bunkers"); 
    echo $x->getADTitle();

    $myarray =$x;//i also tried array_push($$x, $myarray); same error
    echo $myarray[0]->getADTitle();
?>

Fatal error: Cannot use object of type DisplayTaxonomy as array in C:\xampp\htdocs\wordpress\wp-content\plugins\exec-php\includes\runtime.php(42) : eval()'d code on line 9

<?php
 class DisplayTaxonomy {
    public $ADTitle;
 public function setADTitle($ADTitle)
    {
        $this->ADTitle = $ADTitle; 
    }
    public function getADTitle()
    {
        return $this->ADTitle;
    }
?>

Upvotes: 1

Views: 160

Answers (5)

Teekin
Teekin

Reputation: 13259

The reason is because you're assigning the object to $myarray when you do: $myarray = $x. At this point, $myarray is no longer an array, but the object that you assigned to it, namely $x.

In your comment in the code, you say that you also tried array_push($$x, $myarray). The reason that this doesn't work is for two reasons.

Reason 1: You are using $$x instead of $x. If you have two dollar signs ($$x), it is the equivalent of ${$x}, which will first turn $x into a string (probably "Object #1" or something like that), and then look for the variable called: ${'Object #1'}, which obviously doesn't exist. For example, if you have a variable called $something, you can also use it like this: ${'something'}. So if you have a variable that contains the literal string "something", for example $var_name = 'something';, then you could do either ${$var_name} or $$var_name. Both cases will get the content of the variable $something.

Reason 2: You're also putting them in the wrong order in array_push.

This should work instead: array_push($myarray, $x)

But as others have pointed out, this is the exact equivalent: $myarray[] = $x.

I hope that helps explain how things work.

Upvotes: 0

Robert
Robert

Reputation: 8767

As of PHP 5.4 you can also use the short array syntax, which replaces array() with [].

This allows you to both initialize and add objects to an array:

<?php  
class DisplayTaxonomy
{     
    public $ADTitle;  

    public function setADTitle($ADTitle) 
    {         
        $this->ADTitle = $ADTitle;
    }

    public function getADTitle()
    {
        return $this->ADTitle;
    } 
}

$x = new DisplayTaxonomy();
$x->setADTitle("bunkers");
//echo $x->getADTitle();

$myarray[] = $x; // Notice no previous array initialization
echo $myarray[0]->getADTitle(); 
?> 

Upvotes: 1

Francis Avila
Francis Avila

Reputation: 31621

In php, array appending is like this:

$myarray[] = $x

What you are doing is assigning $myarray to your DisplayTaxonomy object, and then trying to use your object as an array with array-subscripting ($myarray[0]). Thus the error.

As an aside, if you are going to use getters and setters for a property, you should not expose that property as public. Try either protected $ADTitle; or just don't use getters and setters.

Upvotes: 0

Waleed Khan
Waleed Khan

Reputation: 11467

$myarray = $x;

should be

$myarray[] = $x

Upvotes: 3

Michael Berkowski
Michael Berkowski

Reputation: 270607

Use the [] operator to append $x onto $myarray:

$myarray[] = $x;
echo $myarray[0]->getADTitle();

Or via array_push(), which as a function call can be a tiny bit less efficient than using the [] lanugage construct.

array_push($myarray, $x);

The methods for defining and adding to arrays are detailed in the PHP manual on Arrays.

Upvotes: 3

Related Questions