interpolation doesn't work for a variable in an array in an object

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

    #1

    interpolation doesn't work for a variable in an array in an object

    within an object function, this code:

    $test2[]='test';
    echo "$test[0]";

    $this->test2[]='test2';
    echo "$this->test2[0]\n";

    produces this result:
    test2
    Array[0]

    Why do I get Array[0] instead of the value in Array[0] ?
    This only seems to happen when the variable is part of an array in an
    object. If, for example, I want to wrap an object's variable in single
    quotes this doesn't work:

    "'$this->test2[0]'"

    I can put double quotes areound the single quotes:

    "'".$this->test2[0]."'"

    but in a long string this becomes very tedious:

    "$this->name=['".$this->mbox[0]."','".$this->mbox[1]."',".$this->mbox[2]."];";

    is there any way to get "'$this->test2[0]'" to work ?

    red




  • red

    #2
    Re: interpolation doesn't work for a variable in an array in an object

    The example I gave made no sense so I'll try again.

    within an object function, this code:

    $test[]='test';
    echo "$test[0]";

    $this->test2[]='test2';
    echo "$this->test2[0]\n";

    produces this result:
    test
    Array[0]

    Why do I get Array[0] instead of the value in Array[0] ?
    This only seems to happen when the variable is part of an array in an
    object. If, for example, I want to wrap an object's variable in single
    quotes this doesn't work:

    "'$this->test2[0]'"

    I can put double quotes areound the single quotes:

    "'".$this->test2[0]."'"

    but in a long string this becomes very tedious:

    "$this->name=['".$this->mbox[0]."','".$this->mbox[1]."',".$this->mbox[2]."];";

    is there any way to get "'$this->test2[0]'" to work ?

    red



    Comment

    • Michael Fesser

      #3
      Re: interpolation doesn't work for a variable in an array in an object

      .oO(red)
      [color=blue]
      >within an object function, this code:
      >
      >$test[]='test';
      >echo "$test[0]";
      >
      >$this->test2[]='test2';
      >echo "$this->test2[0]\n";
      >
      >produces this result:
      >test
      >Array[0]
      >
      >Why do I get Array[0] instead of the value in Array[0] ?[/color]

      The variable is part of the string, but for the PHP parser it ends after
      '$this->test2', which produces the output 'Array'. It works with simple
      variables like $test[0], but with more complex structures (like object
      properties for example) the parser is not always able to detect which
      chars still belong to the variable name and which don't.
      [color=blue]
      >This only seems to happen when the variable is part of an array in an
      >object. If, for example, I want to wrap an object's variable in single
      >quotes this doesn't work:
      >
      >"'$this->test2[0]'"[/color]

      Same as above.
      [color=blue]
      >I can put double quotes areound the single quotes:
      >
      > "'".$this->test2[0]."'"
      >
      >but in a long string this becomes very tedious:
      >
      >"$this->name=['".$this->mbox[0]."','".$this->mbox[1]."',".$this->mbox[2]."];";
      >
      >is there any way to get "'$this->test2[0]'" to work ?[/color]

      Yep:

      Complex (curly) syntax
      <http://www.php.net/manual/en/language.types. string.php#lang uage.types.stri ng.parsing.comp lex>

      Micha

      Comment

      • Steve High

        #4
        Re: interpolation doesn't work for a variable in an array in an object

        Are you instantiating $this->test2 in the constructor?
        i.e.

        $this->test2 = array();


        red <groups@reenie. org> wrote in message news:<LsPWc.339 69$RY5.3660@new s.easynews.com> ...[color=blue]
        > The example I gave made no sense so I'll try again.
        >
        > within an object function, this code:
        >
        > $test[]='test';
        > echo "$test[0]";
        >
        > $this->test2[]='test2';
        > echo "$this->test2[0]\n";
        >
        > produces this result:
        > test
        > Array[0]
        >
        > Why do I get Array[0] instead of the value in Array[0] ?
        > This only seems to happen when the variable is part of an array in an
        > object. If, for example, I want to wrap an object's variable in single
        > quotes this doesn't work:
        >
        > "'$this->test2[0]'"
        >
        > I can put double quotes areound the single quotes:
        >
        > "'".$this->test2[0]."'"
        >
        > but in a long string this becomes very tedious:
        >
        > "$this->name=['".$this->mbox[0]."','".$this->mbox[1]."',".$this->mbox[2]."];";
        >
        > is there any way to get "'$this->test2[0]'" to work ?
        >
        > red[/color]

        Comment

        Working...