Object property variable variable

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

    Object property variable variable

    If I have an object property such as $obj->property_01 = 'blah'

    how do I access it using another variable

    $obj->fred = '01';

    e.g. echo $obj->property_$ob j->fred
    returns 'blah'

    I've tried $obj->property_{$o bj->fred}

    with no luck
  • Rik Wasmus

    #2
    Re: Object property variable variable

    On Tue, 19 Feb 2008 17:30:21 +0100, Hugh Oxford <arestes@fas.co mwrote:
    If I have an object property such as $obj->property_01 = 'blah'
    >
    how do I access it using another variable
    >
    $obj->fred = '01';
    >
    e.g. echo $obj->property_$ob j->fred
    returns 'blah'
    >
    I've tried $obj->property_{$o bj->fred}
    It's not a terrible transparent construct, you might want to avoid this.
    It looks like you could be better served having a property array and
    accessing it like $obj->property[$obj->fred].

    OTOH though, echo $obj->{'property_'.$ obj->fred} should work, I'd advise
    against it.
    --
    Rik Wasmus

    Comment

    • ELINTPimp

      #3
      Re: Object property variable variable

      On Feb 19, 11:30 am, Hugh Oxford <ares...@fas.co mwrote:
      If I have an object property such as $obj->property_01 = 'blah'
      >
      how do I access it using another variable
      >
      $obj->fred = '01';
      >
      e.g. echo $obj->property_$ob j->fred
      returns 'blah'
      >
      I've tried $obj->property_{$o bj->fred}
      >
      with no luck
      $varName = 'property_' . (string) $obj->fred;
      echo $obj->$varName


      ensure the object property has public visibility.

      Possibly a cleaner way is to use getter/setter methods and
      call_user_func. ..just my opinion, though.

      Comment

      • ELINTPimp

        #4
        Re: Object property variable variable

        On Feb 19, 11:37 am, "Rik Wasmus" <luiheidsgoe... @hotmail.comwro te:
        On Tue, 19 Feb 2008 17:30:21 +0100, Hugh Oxford <ares...@fas.co mwrote:
        If I have an object property such as $obj->property_01 = 'blah'
        >
        how do I access it using another variable
        >
        $obj->fred = '01';
        >
        e.g. echo $obj->property_$ob j->fred
        returns 'blah'
        >
        I've tried $obj->property_{$o bj->fred}
        >
        It's not a terrible transparent construct, you might want to avoid this.
        It looks like you could be better served having a property array and
        accessing it like $obj->property[$obj->fred].
        >
        OTOH though, echo $obj->{'property_'.$ obj->fred} should work, I'd advise
        against it.
        --
        Rik Wasmus
        I agree with Rik...especiall y since it seems that you might be
        iterating over the object to get the values of the properties. A
        container (such as an array) is a better solution.

        Comment

        • Hugh Oxford

          #5
          Re: Object property variable variable

          Good idea

          ELINTPimp wrote:
          On Feb 19, 11:37 am, "Rik Wasmus" <luiheidsgoe... @hotmail.comwro te:
          >On Tue, 19 Feb 2008 17:30:21 +0100, Hugh Oxford <ares...@fas.co mwrote:
          >>If I have an object property such as $obj->property_01 = 'blah'
          >>how do I access it using another variable
          >>$obj->fred = '01';
          >>e.g. echo $obj->property_$ob j->fred
          >>returns 'blah'
          >>I've tried $obj->property_{$o bj->fred}
          >It's not a terrible transparent construct, you might want to avoid this.
          >It looks like you could be better served having a property array and
          >accessing it like $obj->property[$obj->fred].
          >>
          >OTOH though, echo $obj->{'property_'.$ obj->fred} should work, I'd advise
          >against it.
          >--
          >Rik Wasmus
          >
          I agree with Rik...especiall y since it seems that you might be
          iterating over the object to get the values of the properties. A
          container (such as an array) is a better solution.

          Comment

          Working...