SitePoint Sponsor

User Tag List

Results 1 to 5 of 5

Thread: Object Array

  1. #1
    SitePoint Addict
    Join Date
    Oct 2005
    Posts
    284
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Object Array

    I am attempting to create an object collection. Though the objects store into the array ok, I am missing a fundamental literacy in how to pull the individual element out of the array as an object.
    Code:
    Class legacy_article_Collection {
     protected $nextRow = 0; // internal index
     protected $rows;        // array of legacy_article_Type s
    
     public function __construct() {
      $this->nextRow = 0;
     }
     
     public function SetRow(legacy_article_Type $row) {
      $rows[] = $row;
      $x = $row->GetArticleTitle();
      echo "<p>legacy_article_CollectionArticle/$arow: $x /</p>\n";
     } // end function
    
     public function GetNextRow() {
      if(count($rows) < $this->nextRow) {
       return false;
      }  // end if
      
       $aRow = $rows[$this->nextRow];
       var_dump($aRow);
      $this->nextRow += 1; 
      return $aRow;   // format is legacy_article_Type
     } // end function
    
    } // end class
    // echo "<p>legacy_article_CollectionArticle { /</p>\n";
    ?>
    The SetRow echos as I expect. But after I populate the array with a dozen legacy_article_Type objects, then invoke the GetNext, I get
    NULL
    As I say, I suspect it is a fundamental illiteracy. But it has me stumped.

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In PHP, you need to explicitly specify $this->rows, if you mean that. Just using $rows will refer to a local (function scope) variable.

  3. #3
    SitePoint Addict
    Join Date
    Oct 2005
    Posts
    284
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    That's part of it, certainly. How embarassing. However, even with that, I seem to be having a hard time trying to pass the object from module to module. Will try again later. Obviously if I'm making scope mistakes like that, it's time to take a break!

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Grnadpa View Post
    Obviously if I'm making scope mistakes like that, it's time to take a break!
    It's usually a good sign.

    I recommend that you program with error_reporting E_ALL. This will make notices and warnings appear as errors, and would have caught this problem (Since you're referring to undefined variables). Just put error_reporting(E_ALL) at the top of your script.

  5. #5
    SitePoint Addict
    Join Date
    Oct 2005
    Posts
    284
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Will do. Thanks for the tip. (And I did figure it out after sleeping on it).


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •