SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 30 of 30
  1. #26
    SitePoint Enthusiast Remy's Avatar
    Join Date
    Oct 2002
    Location
    Amsterdam
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Sork for your anwser !

    I see now how you resolved this, but I can't use your solution. Mine getters/setters could be the same a the db-fieldnames, but this is not always true (and I think it shouldn't).

    So I am going think about other solutions (apart from the one I already posted in this thread). Maby Dr Livingston thought of a another approach?

    -Rémy

  2. #27
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maby Dr Livingston thought of a another approach?
    Umm... I've ran into a few issues myself in regards to something I have being doing and been busy looking for a solution to that instead...

    Sorry I've been no use just now but I've bookmarked this thread and come back to it again... ?

  3. #28
    SitePoint Enthusiast jitao's Avatar
    Join Date
    Feb 2003
    Location
    Shanghai
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Since I'am not alway use the getter and setter to populate an object so here is my version of 'ObjectLoaderIterator', please give more comments

    PHP Code:
    class ObjectListIterator extends Iterator
    {
     
     var 
    $aObject;
     var 
    $aIterator;
     var 
    $returnedObject;
     
     
     
    /**
     *
     *
     *
     *  * $object is the original object you want to get
     * $queryIterator since we are talking about get object 
     * from database this is the reference to 
     * $populatorMethod This is the function name you
     * used to populat your object.
     * $populatorPara is the parameter your populatorMethod need.
     *
     */
     
     
    function ObjectListIterator(&$object, &$queryIterator$populatorMethod=null$populatorPara=null
     {
      
    $this->$aObject=&$object;
      
    $this->$aIterator = & $queryIterator;
     }
     function &
    next() 
     {
      
    $this->aIterator->next();
      
    $row $this->aIterator->getCurrent(); // Only work for QueryIterator so we can assume it is a row;
      
    $returnedObject $this->aObject;//I can't find the clone() method in manual.
      
    if(is_array($populatorPara)) 
      {
          
    $stringBegin='$this->returnedObject->$populator(';
          
    $stringClose ');';
          foreach(
    $populatorPara as $key=>$val
          {
              
    $paraString .='$row[\'$val\'],';
          }
              
    $paraString substr($paraString,0,-1); //remove the last comma
              
    $executeString =$stringBegin.$paraString.$stringClose;
              eval(
    $executeString);
      }
      else
       {
           
    $this->returnedObject->$populator($row[$populatorPara]);
       }
     }
     function 
    reset() 
     {
      
    $this->aIterator->reset();
     }
     function 
    isValid() 
     {
      
    //ofcourse we need some rule to check if it is valid.
     
    }

     function & 
    getCurrent() 
     {
      return 
    $this->returnedObject;
     }
     


    You need bundle all your setters and getters into one function and use it in the iterator.

    What is the solution in Java or other OOP language?
    Ji Tao

  4. #29
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...

    PHP Code:
    foreach($populatorPara as $key=>$val
          {
              
    $paraString .='$row[\'$val\'],';
          }
              
    $paraString substr($paraString,0,-1); //remove the last comma
              
    $executeString =$stringBegin.$paraString.$stringClose
    Looking, I have to ask about the above ?

    First this particular line...

    PHP Code:
    $paraString .='$row[\'$val\'],'
    What happens if at a later date the variable name [ie $row] changes elsewhere ? Is there no way to retreive externally the variable name I'm wondering...

    Not seeing the script which actually generate a database resultset I'd have to think about that myself.

    Second point is the removal of the comma ? I'd put this as a seperate class method altogether since if you need to do more work on this...

    PHP Code:
    '$row[\'$val\'],'
    At a later date no ?

    Umm... Just my thoughts though

  5. #30
    SitePoint Enthusiast Remy's Avatar
    Join Date
    Oct 2002
    Location
    Amsterdam
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry for the late reaction, but didn't want to start a new thread..

    Jitao, nice different approach and with the pointers of Dr Livingston it looks like a fine solution.

    Some other solution come to my mind:
    PHP Code:
    Class Book
    {
        function 
    Book ($id)
        {
            if(
    is_array($id) || is_object($id))
            {
                
    //populate from array or object
            
    }
            else
            {
                
    //populate from source with current id
            
    }
        }

    or a new class that extends this class and where only the constructor is been overriden. What are your thoughts about this solution?

    Quote Originally Posted by jitao
    What is the solution in Java or other OOP language?
    I would like to know that too... Someone who knows this ?

    -Rémy


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
  •