SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Thread: polymorphism?

Hybrid View

  1. #1
    SitePoint Zealot
    Join Date
    Feb 2005
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    polymorphism?

    i want 2 different objects created using the same definition based on parameters.. ie:

    PHP Code:
    $myobject = new Animal'dog' );
    $myotherobject = new Animal'cat' );

    // i now have to instances of same object

    echo $myobject->getNoise(); // will output "mew"
    echo $myotherobject->getNoise(); // will output "bark" 
    right now, i just have a switch statement in the getNoise method based on the parameter in the constructor, but is it possible to keep 2 seperate object definitions using the same declaration?

    php v 4.3.9
    Last edited by Gasolene; Apr 28, 2005 at 14:23.

  2. #2
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    Germany
    Posts
    550
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    why don't you use sth. like
    PHP Code:

    class Animal{
    function 
    getNoise()
    {
         die(
    'Implement in child class!');
    }
    }

    class 
    Cat extends Animal{

    function 
    getNoise()
    {
        return 
    'mew';
    }


  3. #3
    SitePoint Zealot
    Join Date
    Feb 2005
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i know about inheritance, but i was wondering if this can be done using the "same" class name.

  4. #4
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    any particular reason why?

    Inheritance provides you with just the right mechanism - those switch statements cry for decoupling

  5. #5
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What you want is called the Factory pattern. There are many ways to implement it. Here is using a simple switch:
    PHP Code:
    class Animal{
    function 
    Animal($type) {
         switch(
    $type) {
        case 
    'cat':
            return new 
    Cat();
            break;
        case 
    'dog':
            return new 
    Dog();
            break;
        }
    }
    }

    class 
    Cat {

    function 
    getNoise()
    {
        return 
    'mew';
    }
    }

    class 
    Dog {

    function 
    getNoise()
    {
        return 
    'woof';
    }

    Christopher

  6. #6
    SitePoint Zealot
    Join Date
    Feb 2005
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thnx, i'll give that a try


    reaon is, I have a connection object, the idea is to have 1 object that will connect to various data sources (mysql,csv,oracle,ect...).

    I want the same object used for all sources, but the object is getting ery large and full of if statements. i thought there might be a way to seperate the code for each data source without having multiple objects.

    ex: $db = new DAO_Connection( 'source=mysql', $moreoptions... )
    or
    $db = new DAO_Connection( 'source=text', $moreoptions... )

    $rs = $db->open( 'select * from table' );

    while( !$rs->eof() )
    {
    ect...

  7. #7
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I see. Factory pattern, as previosly suggested, is probably the most suitable, but you definetly need a class tree for it. Avoid having such case and if statement in classes.

  8. #8
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I want the same object used for all sources, but the object is getting ery large and full of if statements. i thought there might be a way to seperate the code for each data source without having multiple objects.
    This is the same thing that PEAR DB and others do. I have never found the utility of a Factory for this. Maybe because I don't see the practical difference between the following, and the Factory requires more code and two includes:
    PHP Code:
    $db = new DAO_Connection'source=mysql'$moreoptions... );
    // and

    $db = new DAO_Connection_Mysql($moreoptions... ); 
    As long as the DB objects have the same interface, you can pass a DAO_Connection_Mysql or a DAO_Connection_Oracle and your code will work.
    Christopher

  9. #9
    SitePoint Zealot
    Join Date
    Feb 2005
    Posts
    136
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, there is no programmitic benifit, i just prefered having 1 object for all connections.

    this way I can store the connection string in a config file, and change it that way instead of changing all object names from ...Obj_MySql to ..Obj_Oracle...

    also in that actual object, the connection type and options are sent in 1 parameter.


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
  •