SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 42 of 42
  1. #26
    SitePoint Addict
    Join Date
    Feb 2007
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks much, kyberfabrikken.

    I tried to implement your suggestions and came up with the following...

    PHP Code:
    interface IDbConnection {
        public function 
    prep($query);
        public function 
    exec($query$data null);
    }

    class 
    DbConnection implements IDbConnection {
        private 
    $db;
        public function 
    __construct($host$dbname$user$pass) {
            
    $this->db = new PDO("mysql:host=$host;dbname=$dbname"$user$pass);
        }
        public function 
    prep($query) {
            return 
    $this->db->prepare($query);
        }
        public function 
    exec($query$data null) {
            
    $stmt $this->prep($query);
            
    $stmt->execute($query$data);
            return 
    $stmt;
        }
    }

    class 
    DbConnectionDecorator implements IDbConnection {
        private 
    $decoratedDb;
        public function 
    __construct(IDbConnection $decoratedDb) {
            
    $this->decoratedDb $decoratedDb;
        }
        public function 
    prep($query) {
            return 
    $this->decoratedDb->prep($query);
        }
        public function 
    exec($query$data null) {
            return 
    $this->decoratedDb->exec($query$data);
        }
    }

    class 
    QueryCache extends DbConnectionDecorator {
        private 
    $stmts = array();
        public function 
    prep($query) {
            if (isset(
    $this->stmts[$query])) {
                return 
    $this->stmts[$query];
            }
            
    $this->stmts[$query] = $this->decoratedDb->prep($query);
            return 
    $this->stmts[$query];
        }    
    }

    class 
    ThingLogic {
        private 
    $db;
        public function 
    __construct(IDbConnection $db) {
            
    $this->db $db;
        }
        public function 
    addThing($data) {
            
    $this->db->exec('INSERT INTO...'$data);
        }
    }

    class 
    SiteController {
        private 
    $db;
        public function 
    __construct(IDbConnection $db) {
            
    $this->db $db;
        }
        public function 
    process(Request $request) {
            
    $module $request->getModule();
            if (
    method_exists($this$method 'process'.ucwords($module)) {
                
    $this->$method($request);
            } else {
                throw new 
    InvalidModuleException("Module $method does not exist");
            }
        }
        private function 
    processThing(Request $request) {
            
    $controller = new ThingController(new ThingLogic($this->db));
            
    $controller->process($request);
        }
    }

    $site = new SiteController(new QueryCache(new DbConnection('host''db''user''pass')));
    $site->process(new Request(array('module' => 'thing''action' => 'add'))); 

    I must admit this seems to be quite a bit purtier.

    Bonus: it's the first time I've ever used the decorator pattern outside of assigning toppings to virtual hamburgers!
    Last edited by cuberoot; May 18, 2008 at 11:52.

  2. #27
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Thanks arkinstall -- clearest explanation I've seen to date
    Just wondering, does anybody have any critisism of my post? I'm considering writing an article on OOP for beginners, and I could use that in it.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  3. #28
    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 arkinstall View Post
    Just wondering, does anybody have any critisism of my post? I'm considering writing an article on OOP for beginners, and I could use that in it.
    Perhaps you could be clearer about this:
    # A class is practically a set of variables and methods.
    # An object is a variable which uses the instructions (vars and methods) laid out by the class.
    A class doesn't really have any variables (Except for those statics); It defines which variables will be in the object instance, so it's like a blueprint for an object, but it's wrong to say that it has variables. On the other hand, it is correct that the methods exists in the class and not the object. The distinction between those two may not be as clear with an interpreted languages, such as PHP, but in languages such as C++, the class' code exists in a different position in memory, than the object. Conceptually, the object doesn't have any methods -- It has a class, and that class has methods.

  4. #29
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yeah, I will include that.

    The point I will get across mainly (because I have seen so much misunderstanding about it from beginners) is that a class is simply a source of instructions for the object - the functions and variables of a class don't actually exist, unless they're in an object, as attributes and methods unique to that object.

    It's a set of instructions, and that class can do nothing but follow that set of instructions.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  5. #30
    SitePoint Member Tybe-O's Avatar
    Join Date
    Mar 2008
    Location
    NL, Rotterdam
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Smile

    As for the original question as to why such a programming concept is called a 'class': Take the real world example ship classes...

    http://en.wikipedia.org/wiki/Ship_class

  6. #31
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    On the other hand, it is correct that the methods exists in the class and not the object.
    It's worth noting that this strict distinction between classes and objects only applies to php and similar languages. In more advanced OOP-languages (nomina sunt odiosa "class" is just an object like everything else.

  7. #32
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    In more advanced OOP-languages ... "class" is just an object like everything else.
    Can you clarify that?
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #33
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Just wondering, does anybody have any critisism of my post
    What about:

    > 16 years old - That's 6 years older than Sitepoint

    You can change it to 21 or 22... 16 makes me feel very, very old

  9. #34
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Can you clarify that?
    Sure.

    Code:
    # ruby
    
    # declare a class
    class A
    end
    
    # A is a constant of type Class
    puts A  
    puts A.class
    
    # b is a variable of type Class
    b = A   
    
    # we can call methods on b
    puts b.methods
    puts b.superclass
    
    # 'new' is just a method in class Class
    obj1 = A.new
    obj2 = b.new
    See also
    http://ruby-doc.org/core-1.8.3/classes/Class.html

  10. #35
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bgsomers View Post
    include("config.php");
    $this->indexsize = $indexsize;
    $this->entriesperpage = $entriesperpage;

    is within a function (method?) of a certain class. Must it not be avaiiable within all instances (objects?) of that class?
    Somebody already yelled "NOOOO", but let me elaborate on that.

    It is a most dangerous thing to include code directly into a method's (yes, functions of an object are called methods) code. The whole point of methods (and of regular functions too, but in OOP this is even more pronounced) is to isolate a piece of code and ensure the stability by providing a limited number of entry and exit points, ideally one of each or less.

    Instead of the above, you could have a separate Configuration object(s) which would be passed as an argument to the function that requires them:
    PHP Code:

    class Foo
    {
        function 
    Bar($config)
        {
            
    $this->indexsize $config->indexsize;
            
    $this->entriesperpage $config->entriesperpage;
        }


  11. #36
    SitePoint Addict
    Join Date
    Sep 2006
    Posts
    232
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bgsomers View Post
    Why is an OOP class called a 'class'?
    A nice article about Object-Oriented Programming with PHP

  12. #37
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac View Post
    It is a most dangerous thing to include code directly into a method's [...] code.
    Could you explain this better? Are you sure that's what you meant to say?
    Garcia

  13. #38
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have a class that represents a database connection, would it be that bad to set the connection details at the class level rather then the instance level? Most of the time, you aren't going to use multiple connections, you would instead pass the connection object to objects that need to access a database, but in case you needed multiple connections, what would really be wrong with it?
    Visit my blog
    PHP && Life
    for technology articles and musings.

  14. #39
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hammer65 View Post
    ... but in case you needed multiple connections, what would really be wrong with it?
    What would be wrong with it is the fact that in a design where database connection information is kept in static class variables (such as the one you mention), you would not be able to change the configuration of one connection without affecting all the others, since they all share the same static properties.
    Garcia

  15. #40
    SitePoint Wizard Hammer65's Avatar
    Join Date
    Nov 2004
    Location
    Lincoln Nebraska
    Posts
    1,161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ghurtado View Post
    What would be wrong with it is the fact that in a design where database connection information is kept in static class variables (such as the one you mention), you would not be able to change the configuration of one connection without affecting all the others, since they all share the same static properties.
    but how many times do you do something like that in a single script of a web application?
    Visit my blog
    PHP && Life
    for technology articles and musings.

  16. #41
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ghurtado View Post
    Could you explain this better? Are you sure that's what you meant to say?
    Well yes, I am sure -- I'm just not sure how did you understand it.

    Consider this: If you're including external code into a class, that means that you have some piece of code in a separate file. But does it really have to be in a separate file?

    If you're using this code only on that one place where it's included, there is absolutely no reason why it shouldn't be put directly at the class definition instead of being included. If this code is used in several places, that it is most logical to keep it separate -- but it is still beneficial if it is properly isolated into a class or at least a function of its own and then called from the methods that need it.

    You can think about your code as about electronics equipment. if you have non-isolated, included code it's like a bare electronic circuit which can be inserted anywhere, but each time you have to make sure that you have correctly placed all the connections and soldered all the wires. If your code is properly isolated it's like a hardware module which can easily be plugged in or out at will.

    Isolation is a great help when maintaining applications, and its greater as apps become more complex. Imagine a situation where you have to change something in the included code in order to make it work in the case A, only to discover (maybe weeks later) that it broke something in the case B. If the code was properly isolated it would be simple to alter the function's signature to make the exception when used in the case A and working the same as before from the case B. You can even make separate function for each case and keep a switcher function which is actually called by the classes.

  17. #42
    SitePoint Evangelist ghurtado's Avatar
    Join Date
    Sep 2003
    Location
    Wixom, Michigan
    Posts
    591
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the clarification, I didn't realize you were referring to using the "include" directive within a class method. I agree with you, this is something I would never do; the results would be too unpredictable.
    Garcia


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
  •