arrays containing class types

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

    arrays containing class types

    I am new to PHP. Can I define an array in PHP 4.3 containing class
    types?

    ie. (simplified example)

    class MyType
    {
    function MyFunc()
    {
    $newType = new MyType;
    this->$array_MyTyp e[] = $newType;
    }
    }

    I am getting an error "Cannot use [] for reading"

    Thanks

  • Oli Filth

    #2
    Re: arrays containing class types

    mungflesh wrote:[color=blue]
    > I am new to PHP. Can I define an array in PHP 4.3 containing class
    > types?
    >
    > ie. (simplified example)
    >
    > class MyType
    > {
    > function MyFunc()
    > {
    > $newType = new MyType;
    > this->$array_MyTyp e[] = $newType;
    > }
    > }
    >
    > I am getting an error "Cannot use [] for reading"
    >[/color]

    I would check the syntax for "this", if I were you...

    --
    Oli

    Comment

    • mungflesh

      #3
      Re: arrays containing class types

      yeah sorry - i wrote that off the cuff. the syntax in my script does
      contain $this. i'm a C++ dev by proffession and there's no $s tagged
      onto variables.

      Comment

      • Oli Filth

        #4
        Re: arrays containing class types

        mungflesh wrote:[color=blue]
        > yeah sorry - i wrote that off the cuff. the syntax in my script does
        > contain $this.[/color]

        OK, but have you also removed the "$" in front of array_MyType? i.e.:

        $this->array_MyType[] = $newType;

        --
        Oli

        Comment

        • mungflesh

          #5
          Re: arrays containing class types

          nice one. that's the problem.

          thanks for your help.

          Comment

          • Jerry Stuckle

            #6
            Re: arrays containing class types

            Oli Filth wrote:[color=blue]
            > mungflesh wrote:
            >[color=green]
            >>I am new to PHP. Can I define an array in PHP 4.3 containing class
            >>types?
            >>
            >>ie. (simplified example)
            >>
            >>class MyType
            >>{
            >> function MyFunc()
            >> {
            >> $newType = new MyType;
            >> this->$array_MyTyp e[] = $newType;
            >> }
            >>}
            >>
            >>I am getting an error "Cannot use [] for reading"
            >>[/color]
            >
            >
            > I would check the syntax for "this", if I were you...
            >[/color]

            It should be

            $this->array_MyType = array();
            $this->array_MyType[] = $newtype;

            (Assuming $array_MyType is a class variable)


            --
            =============== ===
            Remove the "x" from my email address
            Jerry Stuckle
            JDS Computer Training Corp.
            jstucklex@attgl obal.net
            =============== ===

            Comment

            Working...