Initialising an array with objects

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nmw
Forum Newbie
Posts: 5
Joined: Tue May 30, 2006 3:19 am

Initialising an array with objects

Post 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]
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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.
Post Reply