php类和数组疯狂至少对我而言

So there are two things that I am stuck on now. First

class DisplayTaxonomy {
public $MyArray[]= new DisplayTaxonomy();  //the compiler says syntax error
//things I have tried 
//public $ss[]= new Object();  


}

Second! in a function like this:

public function whatever()
{
     $Temp = new DisplayTaxonomy();
     $Temp->setADTitle("1");
     $MyArray[]= $Temp;//works just fine.
     //then i tried to return the array 
      return $MyArray[];


}

I get the following //Cannot use [] for reading in C:\xampp\htdocs\wordpress\wp-//content\themes\twentyeleven\page.php on line 52 then in the client side

$y=new DisplayTaxonomy();
$myArray[]=new DisplayTaxonomy();//works fine dont know why I cant do this in theclass.
$myArray[]=$y->getArrayOfDisplayTaxonomyObjects();
echo $myArray[0]->getADTitle();

It seems you want to create a class that handles a collection of Taxonomy objects. In that case you should have two classes, instead of making a class store instances of itself.

class TaxonomyContainer 
{
    private $collection = array();

    public function addElement(DisplayTaxonomy $element)
    {
        $this->collection[] = $element;
    }

    public function getElements()
    {
        return $this->collection;
    }
}

class DisplayTaxonomy
{
    private $adTitle;

    public function setAdTitle($adTitle)
    {
        $this->adTitle = $adTitle;
    }

    //and other functionality the Taxonomy object should have
}

Then you can avoid the ugly self replicating behaviour and separate your concerns.

$container = new TaxonomyContainer();
$element = new DisplayTaxonomy();
$container->addElement($element);

On the next level, it might be worth considering the use of one of PHP's predefined interfaces for the Container class.

Your first issue is due to trying to call the class you're declaring.

class DisplayTaxonomy { 
    public $MyArray[]= new DisplayTaxonomy(); 

You should initialize your object outside of the class, in the portion of code that you need to reference the class.

In addition, the object is already an array so you can omit attaching [] to the end return $MyArray:

public function whatever(){         
    $Temp = new DisplayTaxonomy();
    $Temp->setADTitle("1");

    $MyArray[] = $Temp;

    return $MyArray;
}

You're declaring the array object here:

$MyArray[]= $Temp;//works just fine

You declare objects in the function body and initiate them in the constructor (or a member function). You don't use [] when returning an array, $array[] has the same functionality as array_push, nothing more.

To clarify,

class myClass {

    public $obj = array();

    public function __construct() {
       $this->obj[] = new OtherObject();
    }

    public function getObj() {
       return $this->obj;
    }
}

You can't call code (new DisplayTaxonomy()) when definining class properties. You'll have to assign the value in the constructor of the class:

class Foo
{
    public $MyArray;

    public function __construct()
    {
        $this->MyArray = new DisplayTaxonomy();
    }
}

The other issue is that the $array[] shortcut is for appending to an array. If you want to just return the array (and not write to the array which you're asking about with []), simply skip []:

return $MyArray;

Expanded:

As Vincent Joigƞie pointed out below; you're trying to create a class with the same name as the class you're already creating. This doesn't make any sense, except for static properties. In that case you can't use __construct(), but would rather create / set the object in the static method you're calling to retrieve the object the first time:

static public function getInstance()
{
    if (self::$MyArray === null)
    {
        self::$MyArray = new DisplayTaxonomy();
    }

    return self::$MyArray;
}

This is however probably not what you want, and it seems you've confused something in your logic in your class definition. Guessing freely you might just want:

class Foo
{
    public $MyArray = array();
}

As array() is a static assignment (and not a function call), it's allowed in the class definition.

You cannot do this :

class DisplayTaxonomy {
    public $MyArray[]= new DisplayTaxonomy();
}

because it's like an infinite loop :) So you have to use __contruct() function.

After change the :

return $MyArray[];

to :

return $MyArray;