Adding fields to an object in PHP 4

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

    Adding fields to an object in PHP 4

    Hello,

    I have an object that looks like this:

    shelterrental Object
    (
    [connectionError] =>
    [changedVals] => Array ( )
    [dbh] => Resource id #2
    [id] => 1 [shelter] => 1
    [renter] => Kevin Shakakhnopolis
    [contact] => 504-899-4028
    [date] => 2005-04-11
    )

    I would like to add a field or two under certain circumstances. I have
    tried

    $obj->newfield = 'xxx';
    $obj->{'newfield'} = 'yyy';
    $obj['newfield'] = 'zzz';

    None of these methods work.

    What is the correct syntax to add a field to an object in PHP 4?

    TIA!

    jg


  • Janwillem Borleffs

    #2
    Re: Adding fields to an object in PHP 4

    jerrygarciuh wrote:[color=blue]
    > I would like to add a field or two under certain circumstances. I
    > have tried
    >
    > $obj->newfield = 'xxx';
    > $obj->{'newfield'} = 'yyy';
    >[/color]

    Both of these should work fine, do you get any error messages?

    It would also be useful to see some of the code that generates the initial
    object.


    JW



    Comment

    • jerrygarciuh

      #3
      Re: Adding fields to an object in PHP 4

      "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message
      news:42599403$0 $2277$dbd41001@ news.euronet.nl ...[color=blue]
      > jerrygarciuh wrote:[color=green]
      >> I would like to add a field or two under certain circumstances. I
      >> have tried
      >>
      >> $obj->newfield = 'xxx';
      >> $obj->{'newfield'} = 'yyy';
      >>[/color]
      >
      > Both of these should work fine, do you get any error messages?
      >
      > It would also be useful to see some of the code that generates the initial
      > object.
      >
      >
      > JW[/color]


      JW,

      Thank you for your rapid reply! Here is the function that builds the
      objects from mySQL records. I am thinking my problem is actually caused by
      using foreach on the array of objects and in PHP 4 I can't pass reference
      like foreach ($arr as &$v).

      Any advice most welcome!

      jg

      function &buildObj($resu lt, $classname) {
      if ($result) {
      while($row = mysql_fetch_ass oc($result)) {
      if ($row === null) return null;

      /* Create the object */
      $obj =& new $classname;

      /* Explode the array and set the objects's instance data
      */
      foreach($row as $key => $value)
      {
      $obj->{$key} = $value;
      }
      $objs[] = $obj;
      }
      return $objs;
      } // end if ($result)
      }


      Comment

      • jerrygarciuh

        #4
        Re: Adding fields to an object in PHP 4


        "jerrygarci uh" <designs@no.spa m.nolaflash.com > wrote in message
        news:IDg6e.965$ H53.255@lakerea d05...[color=blue]
        > "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message
        > news:42599403$0 $2277$dbd41001@ news.euronet.nl ...[color=green]
        >> jerrygarciuh wrote:[color=darkred]
        >>> I would like to add a field or two under certain circumstances. I
        >>> have tried
        >>>
        >>> $obj->newfield = 'xxx';
        >>> $obj->{'newfield'} = 'yyy';
        >>>[/color]
        >>
        >> Both of these should work fine, do you get any error messages?
        >>
        >> It would also be useful to see some of the code that generates the
        >> initial object.
        >>
        >>
        >> JW[/color]
        >
        >
        > JW,
        >
        > Thank you for your rapid reply! Here is the function that builds the
        > objects from mySQL records. I am thinking my problem is actually caused
        > by using foreach on the array of objects and in PHP 4 I can't pass
        > reference like foreach ($arr as &$v).
        >
        > Any advice most welcome!
        >
        > jg
        >
        > function &buildObj($resu lt, $classname) {
        > if ($result) {
        > while($row = mysql_fetch_ass oc($result)) {
        > if ($row === null) return null;
        >
        > /* Create the object */
        > $obj =& new $classname;
        >
        > /* Explode the array and set the objects's instance data
        > */
        > foreach($row as $key => $value)
        > {
        > $obj->{$key} = $value;
        > }
        > $objs[] = $obj;
        > }
        > return $objs;
        > } // end if ($result)
        > }
        >[/color]

        My tests confirm that my problem is that foreach operates on a copy and i
        need to pass by reference or use while().


        Comment

        • Janwillem Borleffs

          #5
          Re: Adding fields to an object in PHP 4

          jerrygarciuh wrote:[color=blue]
          > Thank you for your rapid reply! Here is the function that builds the
          > objects from mySQL records. I am thinking my problem is actually
          > caused by using foreach on the array of objects and in PHP 4 I can't
          > pass reference like foreach ($arr as &$v).
          >[/color]

          Passing arguments by reference to foreach would only be a benefit if you
          want to modify them.

          The problem is probably that you are doing something like:

          $obj = buildObj($resul t, "stdClass") ;
          $obj->foo = 'bar';

          This obviously doesn't work, because buildObj isn't returning an object, but
          an array of objects.

          If you would do something like the following, it should work fine:

          $obj[0]->foo = 'bar';


          JW



          Comment

          • jerrygarciuh

            #6
            Re: Adding fields to an object in PHP 4:: Solution


            "jerrygarci uh" <designs@no.spa m.nolaflash.com > wrote in message
            news:rNg6e.967$ H53.934@lakerea d05...[color=blue]
            >
            > "jerrygarci uh" <designs@no.spa m.nolaflash.com > wrote in message
            > news:IDg6e.965$ H53.255@lakerea d05...[color=green]
            >> "Janwillem Borleffs" <jw@jwscripts.c om> wrote in message
            >> news:42599403$0 $2277$dbd41001@ news.euronet.nl ...[color=darkred]
            >>> jerrygarciuh wrote:
            >>>> I would like to add a field or two under certain circumstances. I
            >>>> have tried
            >>>>
            >>>> $obj->newfield = 'xxx';
            >>>> $obj->{'newfield'} = 'yyy';
            >>>>
            >>>
            >>> Both of these should work fine, do you get any error messages?
            >>>
            >>> It would also be useful to see some of the code that generates the
            >>> initial object.
            >>>
            >>>
            >>> JW[/color]
            >>
            >>
            >> JW,
            >>
            >> Thank you for your rapid reply! Here is the function that builds the
            >> objects from mySQL records. I am thinking my problem is actually caused
            >> by using foreach on the array of objects and in PHP 4 I can't pass
            >> reference like foreach ($arr as &$v).
            >>
            >> Any advice most welcome!
            >>
            >> jg
            >>
            >> function &buildObj($resu lt, $classname) {
            >> if ($result) {
            >> while($row = mysql_fetch_ass oc($result)) {
            >> if ($row === null) return null;
            >>
            >> /* Create the object */
            >> $obj =& new $classname;
            >>
            >> /* Explode the array and set the objects's instance
            >> data */
            >> foreach($row as $key => $value)
            >> {
            >> $obj->{$key} = $value;
            >> }
            >> $objs[] = $obj;
            >> }
            >> return $objs;
            >> } // end if ($result)
            >> }
            >>[/color]
            >
            > My tests confirm that my problem is that foreach operates on a copy and i
            > need to pass by reference or use while().
            >[/color]

            function forDate($date) { // $date = Y-m-d
            $shelters = $this->retrieve("faci lity = '$_SESSION[facility]'");
            $srObj = new ShelterRental;
            if ($shelters) {
            while ($s = current($shelte rs) ){
            // retrieve today's rentals by shelter
            $where = array("date = '$date'", "shelter = '$s->id'");
            $rentals = $srObj->retrieve($wher e);
            if ($rentals) {
            echo $s->id;
            foreach ($rentals as $r) {
            $s->renter = $r->renter;
            $s->contact = $r->contact;
            $s->rentalid = $r->id;
            } // foreach
            } // if
            $rentalInfo[] = $s;
            next($shelters) ;
            } // while
            } // if
            return $rentalInfo;
            } // end forDate()


            Comment

            Working...