array as class property question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Julian

    array as class property question

    Hi,

    I am using PHP 4 with classes but I have come across a slight problem...
    I can't declare an array as a property of a class, ie,

    class clTest
    {
    var $a[];

    clTest()
    {
    // ...some stuff here...
    }

    DoAdd($name, $content)
    {
    $this->a[] = array("name" => $id, "content" => $content);
    }
    }

    This produces the error,
    Parse error: parse error, expecting `','' or `';'' in
    /srv/www/htdocs/test/table.php on line 10

    line 10 is the "var $a[]".

    Am I missing something out or doesn't PHP4 allow arrays as properties?

    Julian
  • Erwin Moller

    #2
    Re: array as class property question

    Julian wrote:
    [color=blue]
    > Hi,
    >
    > I am using PHP 4 with classes but I have come across a slight problem...
    > I can't declare an array as a property of a class, ie,
    >
    > class clTest
    > {
    > var $a[];[/color]

    $a = array();

    <snip>
    [color=blue]
    > Am I missing something out or doesn't PHP4 allow arrays as properties?[/color]

    Yes PHP does allow that. :-)
    Allthough most say 'hashed array' or something like that instead of
    properties. (Do you come from Macromedia lingo? ;-) )

    [color=blue]
    >
    > Julian[/color]

    Hope that helps.

    Regards,
    Erwin Moller

    Comment

    • Alvaro G. Vicario

      #3
      Re: array as class property question

      *** Julian escribió/wrote (Fri, 25 Mar 2005 11:55:33 +0000):[color=blue]
      > var $a[];[/color]

      var $a[]='foo';

      or

      var a$;

      or

      var $a=array('foo') ;

      or

      var $a=array();



      --
      -+ Álvaro G. Vicario - Burgos, Spain
      +- http://www.demogracia.com (la web de humor barnizada para la intemperie)
      ++ No envíes tu dudas a mi correo, publícalas en el grupo
      -+ Do not send me your questions, post them to the group
      --

      Comment

      • Julian

        #4
        Re: array as class property question

        Thanks, it now works.
        [color=blue]
        > (Do you come from Macromedia lingo? ;-) )[/color]

        No, I'm from a C/C++ background but thought I'd give PHP a go. The
        "properties " is the term used in my PHP reference book.

        Regards,
        Julian

        Comment

        Working...