Page 1 of 1

Initialising an array with objects

Posted: Tue May 30, 2006 3:25 am
by nmw
twigletmac | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Can this be done? I'm trying to initialise an array variable $x which is a static member of a class. As follows:

Code: Select all

class CMyObj
{
    var    $a;
    var    $b;
 
    function
            __construct($a,$b)
    {
        $this->a = $a;
        $this->b = $b;
    }
 
}

Code: Select all

var        $x = array(new CMyObj(1,2),new CMyObj(3,4));

I've tried various incantations of the syntax but I can't make this work. I believe array() is supposed to take key=>value pairs, or values alone (implied numeric keys), and value supposedly can be anything, therefore why not an object?
:?:


twigletmac | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue May 30, 2006 5:07 am
by Oren
You are mixing the old kind of OOP with the new one, PHP 5 should accept it anyway but I just thought I'd let you know.
I'm pretty new to OOP too, but I'm pretty sure that the second part of your code is wrong, it can't be done that way as far as I know.