SitePoint Sponsor

User Tag List

Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 51
  1. #26
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    My problem is that I assumed that using the Eclipse "Database" and
    "QueryResult" classes was essentially the same as implementing a Dao.
    I've seen a lot of discussions on Dao here but thought, "Ah, that's
    what I'm doing with eclipse". So I didn't bother to read the Dao stuff
    throughly.
    I should know by now that assuming things like that is not a good idea.

    I don't understand all the code that's been posted above, but I guess
    it will eventually sink in. :)

    I *do* think I understand the general idea though.


    ===================

    Question regarding codezilla's code:


    Do you think it would be ok to change the Dao class a bit to work
    with an Eclipse "Database" object.

    The Dao constructor might look like:

    PHP Code:
    class Dao {
        
        function 
    Dao(&$dbObj
        {    
        
    // $this->dbObj will replace $this->conn
        // I'll have to go through code that uses $this->conn
        // to work with my Eclipse object.
            
    $this->dbObj =& $dbObj
        } 
    }


    class 
    BidderDao extends Dao {
        
        function 
    BidderDao(&$dbObj)
        {   
    parent::Dao($dbObj);
        }
    }


    -------

    $dbObj =& new MyDatabase('dbName''hostName');
    $dbObj->connect('userName''passWord')
    $daoObj =& new BidderDao($dbObj); 
    thanks,
    --ed
    Last edited by coo_t2; Jun 11, 2003 at 12:05. Reason: forgot to put "extends Dao" in BidderDao definition.

  2. #27
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by codezilla
    Not sure if your implying '$this->SuperClass();' won't work in my example
    Not at all, your method works find for any inherited method (or methods of the child class itself), just wanted to point out the parent:: operator must be available to access overridden methods.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  3. #28
    SitePoint Zealot codezilla's Avatar
    Join Date
    Nov 2002
    Location
    upstairs
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, cool, then we're saying the same thing.

  4. #29
    SitePoint Zealot codezilla's Avatar
    Join Date
    Nov 2002
    Location
    upstairs
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by coo_t2
    Do you think it would be ok to change the Dao class a bit to work with an Eclipse "Database" object.
    Absolutely! You'll need to change all the database interaction code in my example since it's quite different from the ECLIPSE classes. Good luck!

  5. #30
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Confused ? I am now But thanks folks, I'll have a read over this thread off-line later and try to remember the number of ways how to use :: Codezilla - Thanks for reminding me about the post I have above on creating an object instance of a class and actually using a class method Oppss ?

  6. #31
    SitePoint Member
    Join Date
    May 2003
    Location
    Paris, France
    Posts
    3
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Would it be possible to post DataSource.class.php and ArrayList.class.php so we can run the example ?

  7. #32
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Gilles
    Would it be possible to post DataSource.class.php and ArrayList.class.php so we can run the example ?
    He attached part of his library here:
    http://sitepointforums.com/showpost....9&postcount=45

    But ArrayList.class.php isn't part of it.
    DataSource.class.php is though.

    I think that's just the DB portion of his library.

    --ed

  8. #33
    SitePoint Addict
    Join Date
    Aug 2002
    Location
    Ottawa, Ontario, Canada
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    For a static method to work, you can not have any references to $this in the method (because there is no instance). Other than that, they work the same (i.e. take parameters, return results, must have the class included before use, etc.).

    HTH
    PHP Code:
    <?php
    class Foo{
      var 
    $x=1;
      
      
    echo_x(){
        echo 
    $this->x;
      }
      
      
    increment(){
        
    Bar::increment();
      }
    }
    class 
    Bar{
      
    increment(){
        
    $this->x++;
      }
    }
    $demo=&new Foo();
    $demo->echo_x(); //echos 1
    $demo->increment();
    $demo->echo_x(); //echos 2
    ?>
    The $this reference in the static call above works fine

    I know, I know, I am being nitpicky... but I just wanted to point out that it can be used like above.

    Cheers,
    Keith.

  9. #34
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Appearantly only because the static call is embeded within another classes method where the variable $this is in scope.

    Try:
    PHP Code:
    <?php 
    error_reporting
    (E_ALL);

    class 
    Foo{
      var 
    $x=1;
      
      function 
    echo_x(){
        echo 
    $this->x;
      }
      
      function 
    increment(){
        
    Bar::increment();
      }
    }
    class 
    Bar{
      function 
    increment(){
        
    $this->x++;
      }
    }
    $demo=&new Foo();
    $demo->echo_x(); //echos 1
    $demo->increment();
    $demo->echo_x(); //echos 2
    Bar::increment();

  10. #35
    SitePoint Addict
    Join Date
    Aug 2002
    Location
    Ottawa, Ontario, Canada
    Posts
    214
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I know that... I was just trying to bug you a bit

    Cheers,
    Keith.

  11. #36
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Actually seems like a PHP bug to me. I don't think the Foo $this should be in scope for the Bar static call oh well, chalk it up to another nuance of my favorite interpreted language

  12. #37
    SitePoint Member
    Join Date
    Oct 2003
    Location
    London, UK
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question

    Can you post the DataSource and ArrayList classes referenced below so I can get a complete picture of what's happening here.

    Thanks

    Quote Originally Posted by codezilla
    Sounds like you're ready for a Data Access Object (DAO). DAOs are great because you can keep all the SQL necessary for a domain model class isolated in a single DAO class. Generally speaking, you have one DAO class per database table.

    Here's some extensive example code for a hypothetical Bidder class that I adapted from my own class library/framework.


    Dao.class.php:
    PHP Code:
    require_once('DaoRegistry.class.php');
    require_once(
    'DataSource.class.php');
    require_once(
    'ArrayList.class.php');

    class 
    Dao
    {

        var 
    $conn;
        
        function 
    Dao($dsn)
        {
            
    $this->conn =& DataSource::getConnection($dsn);
        }
        
        function &
    abstractFind($id)
        {
            
    $obj =& DaoRegistry::get($this$id);
            if (
    is_null($obj))
            {
                
    $stmt =& $this->conn->prepareStatement($this->findStatement());
                
    $stmt->setInt(1$id);
                
    $rs =& $stmt->executeQuery();
                
    $rs->next();
                
    $obj =& $this->load($rs);
            }
            return 
    $obj;
        }
        
        function 
    delete(&$obj)
        {
            if (! 
    is_null($obj))
            {
                
    $this->doDelete($obj);
            }    
        }
        
        function 
    doDelete(&$obj)
        {
        }
        
        function 
    doInsert(&$obj)
        {
        }
        
        function &
    doLoad($id, &$rs)
        {
        }
        
        function 
    doUpdate(&$obj)
        {
        }
        
        function 
    find()
        {
        }
        
        function 
    findStatement()
        {
        }
        
        function 
    insert(&$obj)
        {
            if (! 
    is_null($obj))
            {
                
    $obj->setId($this->doInsert($obj));
                
    DaoRegistry::put($this$obj->getId(), $obj);
                return 
    $obj->getId();
            }
        }
        
        function &
    load(&$rs)
        {
            
    $id $rs->getInt('id');
            
    $obj =& DaoRegistry::get($this$id);
            if (
    is_null($obj))
            {
                
    $obj =& $this->doLoad($id$rs);
                
    DaoRegistry::put($this$obj->getId(), $obj);
            }
            return 
    $obj;
        }
        
        function &
    loadAll(&$rs)
        {
            
    $objList =& new ArrayList();
            while (
    $rs->next())
            {
                
    $objList->append($this->load($rs));
            }
            return 
    $objList;
        }
        
        function 
    update(&$obj)
        {
            if (! 
    is_null($obj))
            {
                
    $this->doUpdate($obj);
            }
        }
        

    Since this post is already really long I won't inlcude the source for DaoRegistry.class.php, DataSource.class.php, ArrayList.class.php

    Usage example:
    PHP Code:
    $dao =& new BidderDao();
    $list =& $dao->findForAuctionId($_GET['auction_id']);
    $i =& $list->iterator();
    while (
    $i->hasNext())
    {
        
    $bidder =& $i->next();
        
    // do stuff with $bidder


  13. #38
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...so I can get a complete picture of what's happening here.
    I would be interested also to see the complete classes

    Many Thanks

  14. #39
    SitePoint Zealot codezilla's Avatar
    Join Date
    Nov 2002
    Location
    upstairs
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No problem.

    PHP Code:
    class DataSource
    {

        var 
    $connections = array();

        function 
    DataSource()
        {
        }


        function &
    getConnection($dsn)
        {
            
    $this =& DataSource::_getInstance();
            if (! isset(
    $this->connections[$dsn]))
            {
                include_once(
    'Connection.class.php');
                
    $url parse_url($dsn);
                
    $this->connections[$dsn] =& new Connection(
                    (isset(
    $url['scheme']) ? $url['scheme'] : ''),
                    (isset(
    $url['host'])   ? $url['host'] : 'localhost') . 
                        (isset(
    $url['port']) ? ':' $url['port'] : ''),
                    (isset(
    $url['path'])   ? substr($url['path'], 1) : ''),
                    (isset(
    $url['user'])   ? $url['user'] : ''),
                    (isset(
    $url['pass'])   ? $url['pass'] : ''));
            }
            return 
    $this->connections[$dsn];
        }
        

        function &
    _getInstance()
        {
            static 
    $instance;
            if (
    is_null($instance))
            {
                
    $instance = new DataSource();
            }
            return 
    $instance;
        }
        

    I don't use ArrayList anymore (in favor of regular ol' arrays), and I've refactored out all references to it, but here it is anyway:

    PHP Code:
    class ArrayList
    {

        var 
    $arr;

        function 
    ArrayList()
        {
            if (
    func_num_args() == 1)
            {
                
    $this->arr ArrayList::valueOf(func_get_arg(0));
            }
        }

        function 
    add($index, &$element)
        {
            
    $this->arr array_splice($this->arr$index0, array($element));
        }
        
        function 
    addAll($index, &$list)
        {
            
    $this->arr array_splice($this->arr$index0$list->toArray());
        }
        
        function 
    append(&$element)
        {
            
    $this->arr[] =& $element;
        }
        
        function 
    appendAll(&$list)
        {
            
    $this->arr array_merge($this->arr$list->toArray()); 
        }
        
        function 
    clear()
        {
            unset(
    $this->arr);
            
    $this->arr = array();
        }
        
        function 
    contains(&$element)
        {
            return 
    in_array($element$this->arrTRUE);
        }
        
        function 
    containsAll(&$list)
        {
            foreach (
    $list->toArray() as $element)
            {
                if (! 
    in_array($element$this->arrTRUE))
                {
                    return 
    FALSE;
                }
            }
            return 
    TRUE;
        }
        
        function &
    get($index)
        {
            return 
    $this->arr[$index];
        }
        
        function &
    getFirst()
        {
            return 
    $this->arr[0];
        }
        
        function &
    getLast()
        {
            return 
    $this->arr[count($this->arr) - 1];
        }
        
        function 
    indexOf(&$element)
        {
            
    $index array_search($element$this->arr);
            if (
    $index !== FALSE)
            {
                return 
    $index;
            }
            return -
    1;
        }
        
        function 
    isEmpty()
        {
            return (
    count($this->arr) == 0);
        }
        
        function &
    iterator()
        {
            return 
    $this->listIterator(0);
        }
        
        function 
    lastIndexOf(&$element)
        {
            
    $pos array_search($elementarray_reverse($this->arr));
            if (
    $pos !== FALSE)
            {
                return 
    count($this->arr) - $pos;
            }
            return -
    1;
        }
        
        function &
    listIterator($fromIndex 0)
        {
            include_once(
    'ArrayListIterator.class.php');
            return new 
    ArrayListIterator($this$fromIndex);
        }
        
        function 
    prepend(&$element)
        {
            
    array_unshift($this->arr, array($element));
        }
        
        function 
    prependAll(&$list)
        {
            
    array_unshift($this->arr$list->toArray());
        }
        
        function &
    remove($index)
        {
            
    $element =& $this->arr[$index];
            
    array_splice($this->arr$index1);
            return 
    $element;
        }
        
        function 
    removeAll(&$list)
        {
            
    $size count($this->arr);
            
    $list $list->toArray();
            
    $this->arr array_diff($this->arr$list);
            return (
    count($this->arr) != $size);
        }
        
        function &
    removeFirst()
        {
            
    $element =& $this->arr[0];
            
    array_shift($this->arr);
            return 
    $element;
        }
        
        function &
    removeLast()
        {
            
    $element =& $this->arr[$this->size() - 1];
            
    array_pop($this->arr);
            return 
    $element;
        }
        
        function 
    removeRange($fromIndex$toIndex)
        {
            
    array_splice($this->arr$fromIndex$toIndex $fromIndex);
        }
        
        function 
    retainAll(&$list)
        {
            
    $size count($this->arr);
            
    $list $list->toArray();
            
    $this->arr array_intersect($this->arr$list);
            return (
    count($this->arr) != $size);
        }
        
        function &
    set($index, &$element)
        {
            
    $oldElement =& $this->get($index);
            
    $this->arr[$index] =& $element;
            return 
    $oldElement;
        }
        
        function 
    size()
        {
            return 
    count($this->arr);
        }
        
        function &
    subList($fromIndex$toIndex)
        {
            
    $fromIndex = (int) $fromIndex;
            
    $toIndex = (int) $toIndex;
            if (
    $toIndex $fromIndex)
            {
                return 
    NULL;
            }
            return new 
    ArrayList(array_slice($this->arr$fromIndex$toIndex $fromIndex));
        }

        function &
    toArray()
        {
            return 
    $this->arr;
        }

        function 
    valueOf($arg)
        {
            if (
    is_object($arg) && method_exists($arg'toArray'))
            {
                return 
    array_values($arg->toArray());
            }
            return 
    array_values((array) $arg);
        }


    Last edited by codezilla; Nov 17, 2003 at 11:00.

  15. #40
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just out of curiosity, and even though I suspect that I know the answer, why did you stop using it?

  16. #41
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also where is the OBJECT class that the ArrayList extends ? Umm...

  17. #42
    SitePoint Zealot codezilla's Avatar
    Join Date
    Nov 2002
    Location
    upstairs
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ghurtado
    Just out of curiosity, and even though I suspect that I know the answer, why did you stop using it?
    I actually had classes for all the native data types, but I was persuaded that they introduced too much overhead and are simply not worth the trouble. Or, more concisely, PHP != Java.
    Last edited by codezilla; Nov 17, 2003 at 14:19.

  18. #43
    SitePoint Zealot codezilla's Avatar
    Join Date
    Nov 2002
    Location
    upstairs
    Posts
    110
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    Also where is the OBJECT class that the ArrayList extends ? Umm...
    Oops, removed that extends stuff. That's partially why I didn't want to post them, because many classes introduce further dependencies. My base Object class doesn't have much going on, and isn't worth mentioning. Every time I refactor it, it becomes smaller and will probably disappear completely eventually.

  19. #44
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP != Java
    Maybe... PHP > Java ?

  20. #45
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you Codezilla, very much the answer I was looking for

    You are exactly right, Java might need a properly powerful Array class, since the language itself does not provide as many facilities to work with arrays. PHP is way ahead of the game there.

  21. #46
    SitePoint Member
    Join Date
    Oct 2003
    Location
    London, UK
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Factory or Abstract Factory?

    Following the excellent DAO examples from Codezilla in this thread I wondered if I can provoke similar thoughts on the best way to introduce a Factory/Abstract Factory class (not really sure which one or even if it's the right pattern) to allow different databases to be plugged in and out transparently.

    I see PEAR and other database abstractions use different 'driver' files for each supported database, but it's very messy, and cleary not object-oriented. I guess users of PEAR DB don't care what's under the hood if it works but I have a real mental block with that approach.

    The otherwise excellent Eclipse classes from Vincent Oostind also seem to be missing this capability unless you put a switch statement over the top which seems just as bad as PEAR DB.

    I'm guessing the Connection object is the right place to introduce the Factory(?) object. The Factory then returns the correct 'driver' based on the scheme portion of the original DSN. But then again, I am possibly taking completely the wrong approach.

    Any thoughts very welcome.

  22. #47
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...to allow different databases to be plugged in and out transparently.
    Wouldn't the Adapter Design Pattern be far, far more appropriate for this no ?

    Sure Harry covered this over at his site not so long ago in fact... Had a look myself and it's well smart in fact

    Have a look at Harry's site for the Adapter Pattern and you'll proberly understand my point yes ?

    --EDIT--

    For the correct use of a Factory class have a look over at www.javaworld.com for their design patterns. I was looking at this pattern yersterday and from what I can tell this wouldn't be the pattern your looking for imho...

  23. #48
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by gzhlon
    Following the excellent DAO examples from Codezilla in this thread I wondered if I can provoke similar thoughts on the best way to introduce a Factory/Abstract Factory class (not really sure which one or even if it's the right pattern) to allow different databases to be plugged in and out transparently.

    I see PEAR and other database abstractions use different 'driver' files for each supported database, but it's very messy, and cleary not object-oriented. I guess users of PEAR DB don't care what's under the hood if it works but I have a real mental block with that approach.

    The otherwise excellent Eclipse classes from Vincent Oostind also seem to be missing this capability unless you put a switch statement over the top which seems just as bad as PEAR DB.

    I'm guessing the Connection object is the right place to introduce the Factory(?) object. The Factory then returns the correct 'driver' based on the scheme portion of the original DSN. But then again, I am possibly taking completely the wrong approach.

    Any thoughts very welcome.

    As you know to get a DB object with eclipse, you'd do something like
    the following:

    $dbObj =& MySqlDatabase(..info...);

    if I want to change the DB, I just make a change to which object is instantiated:

    $dbObj =& OracleDatabase(...info...);

    So what would be the benefit of creating an abstract layer on top of this?
    If you make a connection through another abstract layer, you still have to send
    the type of DB being used as a parameter, don't you? So why not just change
    the object name that's being instantiated?

    THe only way I see a benefit is if you want people to be able to create a connection to a DB without ever knowing which db.
    Maybe like so:

    // don't care which type of DB is being used under the hood.
    $dbObj =& DbConnection(..non-specific DB type info..);

    Other than that, I could be missing something. If so, please enlighten me


    --ed

  24. #49
    SitePoint Member
    Join Date
    Oct 2003
    Location
    London, UK
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    coo t2, you're dead right! I was having trouble seeing this clearly, probably a result of reading too many ill conceived articles on the subject elsewhere.

    In fact, codezilla's classes use ADOdb which provide the type of abstraction I was looking for in the first place. The scheme portion of the DSN in codezilla's classes provide ADOdb with which 'type' of underlying database technology to use.

    Forgive my ignorance and thanks for the responses.

  25. #50
    SitePoint Member
    Join Date
    Oct 2003
    Location
    London, UK
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi Codezilla!

    I have gone a long way to getting your classes to work on my own system and I am very pleased with the results.

    But in the usage example below I cannot see where the iterator comes from. It's not defined as a method/property of the DAO class or derived classes.

    Please could you explain this?

    TIA


    Quote Originally Posted by codezilla

    Usage example:
    PHP Code:
    $dao =& new BidderDao();
    $list =& $dao->findForAuctionId($_GET['auction_id']);
    $i =& $list->iterator();
    while (
    $i->hasNext())
    {
        
    $bidder =& $i->next();
        
    // do stuff with $bidder


    ps. Any idea when your BSOC library will be released or can you provide a sneak preview?


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
  •