Making PHP Classes Play Nicely With Each Other (OOP Pattern Question)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • centenial
    New Member
    • Oct 2008
    • 1

    #1

    Making PHP Classes Play Nicely With Each Other (OOP Pattern Question)

    Hi all,

    I have a PHP OOP design question. I've done some searching in google, but wasn't able to turn up anything concrete. I'm hoping some experts can point me to the most elegant solution.

    I have a set of classes. All of my classes "extend" an abstract class called Base. (This class reads a config file, has a basic set of common methods, and sets up needed parameters)

    Most of my classes need a class called Mysql to perform database operations.

    Is the best way to access the Mysql class to pass it in the __constructor in each of my child classes, or to instantiate it in my Base class, and assign it to a public property? Or perhaps there is a better way altogether?

    I'm really looking for the best way to do this, so thanks in advance for your advice!
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    One common way to incorporate a db class is using the singleton pattern (though there are others like the registry pattern). in this case you query the class with the class call (e.g. mysql::connect( ) (connect to the db (will do nothing if connection is already established); mysql::query($q uery))

    regards

    Comment

    • labmonkey111
      New Member
      • Sep 2008
      • 44

      #3
      I think it would be a much better design to have it in the base class. The whole point of inheritance is to cut down on code duplication and share code among classes. But I would recommend making the Mysql object protected in the base class. That way only that class and all subclasses can access it. Its a little more secure that way.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        Hi.

        What I usually did was to create an instance of my database class and pass it into the classes that need it. (usually via the constructor).
        This way there is only one instance of the class in play.

        Another method I have used in the past was to have the class extend my database class, which would allow me to use the database class as a part of the current class.
        This is obviously not a good idea if you use lots of objects in your code, but can be made to work well in smaller projects.

        Then you could always use a static approach, creating a static class that could be used everywhere without actually creating an object instance to pass around.
        This is probably the simplest way, but for some reason not that appealing. (Kind of feels like abusing the global scope :P)

        A compromise between the two could be to create a static class that creates or returns an existing database object. That way you would only have a single instance of your object in play, without having to pass it around. It could simply be fetched wherever it is needed.

        Somewhat like:
        [code=php]
        class MyDatabase {
        private static $_link;
        public static function GetConnection() {
        if(self::$_link == null){
        self::$_link = new mysqli("host", "usr", "paswd", "db");
        if(mysqli_conne ct_errno()) {
        throw new Exception("Data base connection failed: ". mysqli_connect_ error());
        }
        }
        return self::$_link;
        }
        }

        class foo {
        public function DoSomething(){
        $dbLink = MyDatabase::Get Connection();
        $result = $dbLink->query("SHOW TABLES");
        if($result) {
        while($row = $result->fetch_array( )) {
        echo " - " . $row[0] ."<br />";
        }
        }
        $result->close();
        }
        }
        [/code]
        This is my favorite at the moment :)

        Comment

        • Atli
          Recognized Expert Expert
          • Nov 2006
          • 5062

          #5
          Originally posted by labmonkey111
          I think it would be a much better design to have it in the base class. The whole point of inheritance is to cut down on code duplication and share code among classes. But I would recommend making the Mysql object protected in the base class. That way only that class and all subclasses can access it. Its a little more secure that way.
          A good point, but...

          If you have your base class initialize a new instance of the database class for every new class, then you may end up with a bunch of open database connections for each execution of your code, which could affect performance.

          A few lines of repeated code would be better than that, wouldn't you agree?

          Comment

          • labmonkey111
            New Member
            • Sep 2008
            • 44

            #6
            Originally posted by Atli
            A few lines of repeated code would be better than that, wouldn't you agree?
            Yes, that sounds a lot better. I was thinking purely in terms of OOP, obviously databases change things a little.

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              Originally posted by Atli
              This is my favorite at the moment
              as I said, a Singleton.

              If you have the PEAR package installed, you can also use one of PEAR's db classes.

              regards

              Comment

              Working...