Build string to reference object

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

    Build string to reference object

    I want to build a string to reference an object.

    I can reference is manually thus:

    print_r($this->struct->parts[0]->parts[1]);

    but if I build a string...

    $string = "->parts[0]->parts[1]"

    ....and try to reference it thus...

    print_r($this->struct{$string });


    I get Fatal error : Cannot use object of type stdClass as array

    Any thoughts?
  • Jerry Stuckle

    #2
    Re: Build string to reference object

    Hugh Oxford wrote:
    I want to build a string to reference an object.
    >
    I can reference is manually thus:
    >
    print_r($this->struct->parts[0]->parts[1]);
    >
    but if I build a string...
    >
    $string = "->parts[0]->parts[1]"
    >
    ...and try to reference it thus...
    >
    print_r($this->struct{$string });
    >
    >
    I get Fatal error : Cannot use object of type stdClass as array
    >
    Any thoughts?
    True. You can't do it that way. You could use eval() - but that's a
    bad thing to use.

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

    Comment

    • petersprc

      #3
      Re: Build string to reference object

      I would say, typically this isn't necessary, as you can pass around a
      reference to the nested objects that you want to update or you map
      strings to objects.

      If you have a large app which requires this type of dynamic path-based
      access, you might consider using an XML document data structure and
      querying it with XPath queries.

      In general, your example could be directly implemented using either
      eval:

      $str = 'parts[0]->parts[1]';
      eval("\$val = \$this->struct->$str;");
      print_r($val);

      Or with a function getNestedProper ty like so:

      <?

      function getNestedProper ty($obj, $path)
      {
      {
      $val = $obj;
      foreach ($path as $part) {
      $val = is_object($val) ? $val->$part :
      $val[$part];
      }
      return $val;
      }

      class c
      {
      function c()
      {
      $this->arr = array('a' =array(1, 2, 3));
      }
      }

      print_r($val = getNestedProper ty(new c,
      array('arr', 'a', 1)));

      ?>

      Regards,

      John Peters

      On May 28, 8:51 am, Hugh Oxford <ares...@fas.co mwrote:
      I want to build a string to reference an object.
      >
      I can reference is manually thus:
      >
      print_r($this->struct->parts[0]->parts[1]);
      >
      but if I build a string...
      >
      $string = "->parts[0]->parts[1]"
      >
      ...and try to reference it thus...
      >
      print_r($this->struct{$string });
      >
      I get Fatal error : Cannot use object of type stdClass as array
      >
      Any thoughts?

      Comment

      • Hugh Oxford

        #4
        Re: Build string to reference object

        petersprc wrote:
        I would say, typically this isn't necessary, as you can pass around a
        reference to the nested objects that you want to update or you map
        strings to objects.
        >
        If you have a large app which requires this type of dynamic path-based
        access, you might consider using an XML document data structure and
        querying it with XPath queries.
        >
        In general, your example could be directly implemented using either
        eval:
        >
        $str = 'parts[0]->parts[1]';
        eval("\$val = \$this->struct->$str;");
        print_r($val);
        >
        Or with a function getNestedProper ty like so:
        >
        <?
        >
        function getNestedProper ty($obj, $path)
        {
        {
        $val = $obj;
        foreach ($path as $part) {
        $val = is_object($val) ? $val->$part :
        $val[$part];
        }
        return $val;
        }
        >
        class c
        {
        function c()
        {
        $this->arr = array('a' =array(1, 2, 3));
        }
        }
        >
        print_r($val = getNestedProper ty(new c,
        array('arr', 'a', 1)));
        >
        ?>
        >
        Regards,
        >
        John Peters
        >
        On May 28, 8:51 am, Hugh Oxford <ares...@fas.co mwrote:
        >I want to build a string to reference an object.
        >>
        >I can reference is manually thus:
        >>
        >print_r($thi s->struct->parts[0]->parts[1]);
        >>
        >but if I build a string...
        >>
        > $string = "->parts[0]->parts[1]"
        >>
        >...and try to reference it thus...
        >>
        >print_r($thi s->struct{$string });
        >>
        >I get Fatal error : Cannot use object of type stdClass as array
        >>
        >Any thoughts?
        >
        Thanks Peter and for your other replies elsewhere.

        Comment

        Working...