Is this correct OO implementation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jibbs

    #1

    Is this correct OO implementation

    I am designing a site and I was just wondering if the folowing class I
    designed, which is fairly simple, should be used.

    <?php
    class DB {

    private $username;
    private $password;
    private $db;
    private $server;


    //contruct
    function __contruct(stri ng $username,$pass word,$server,st ring $db){
    $this->username = $username;
    $this->password = $password;
    $this->db = $db;
    $this->server = $server;
    }

    //connect
    static connect () {
    @mysql_connect( $this->server,$this->username,$th is->password) or
    die ("Could not establish a connection with the MySQL
    database.\n");

    @mysql_select_d b($this->db) or
    die ("Could not select the database" . $this->db . "\n");
    }

    //getter methods
    function get_username(){
    return $this->username;
    }

    function get_password (){
    return $this->password;
    }

    function get_db(){
    return $this->db;
    }

    function get_server (){
    return $this->server;
    }

    //setter methods

    function set_username ($username){
    $this->username = $username;
    }

    function set_password ($password){
    $this->password = $password;
    }

    function set_db ($db){
    $this->db = $db;
    }

    function set_server ($server){
    $this->server = $server;
    }
    }

    ?>

    I figured if I wanted to change which database to use I could do it
    quickly with a setter method, check it with a getter method. Also,
    apply this to usernames and passwords and servers.
    Is this too much code for a simple task?

  • Jibbs

    #2
    Re: Is this correct OO implementation

    I just realized that it should be static function connect.
    Sorry about the typo

    Comment

    • porneL

      #3
      Re: Is this correct OO implementation


      I think you should try to connect to the database in costructor and throw
      exception if it fails.

      database object without database connection is useless.
      die() is not OO style.


      --
      porneL

      Comment

      • Jibbs

        #4
        Re: Is this correct OO implementation

        <?php
        class sqlException extends Exception{
        function __construct($ex ception){
        parent::Excepti on($exception);
        }
        }

        class sqlConnect{

        private $username;
        private $password;
        private $db;
        private $server;
        private $link;


        //contruct
        function __construct($us ername,$passwor d,$server,$db){
        $this->username = $username;
        $this->password = $password;
        $this->db = $db;
        $this->server = $server;
        $this->link =
        @mysql_connect( $this->server,$this->username,$th is->password)

        if(!$this->link){
        throw new sqlException(my sql_error());
        }

        if(!@mysql_sele ct_db($this->db){
        throw new sqlException(my sql_error());
        }

        }

        //destruct
        function __destruct(){
        mysql_close($th is->link);
        }

        ?>


        Better?

        Comment

        • Jibbs

          #5
          Re: Is this correct OO implementation

          <?php
          class sqlException extends Exception{
          function __construct($ex ception){
          parent::Excepti on($exception);
          }
          }

          class sqlConnect{

          private $username;
          private $password;
          private $db;
          private $server;
          private $link;


          //contruct
          function __construct($us ername,$passwor d,$server,$db){
          $this->username = $username;
          $this->password = $password;
          $this->db = $db;
          $this->server = $server;
          $this->link =
          @mysql_connect( $this->server,$this->username,$th is->password)

          if(!$this->link){
          throw new sqlException(my sql_error());
          }

          if(!@mysql_sele ct_db($this->db){
          throw new sqlException(my sql_error());
          }

          }

          //destruct
          function __destruct(){
          mysql_close($th is->link);
          }

          ?>

          Better?

          Comment

          • Jibbs

            #6
            Re: Is this correct OO implementation

            Pastebin.com is the number one paste tool since 2002. Pastebin is a website where you can store text online for a set period of time.


            I am wondering if my throws are right and i can use mysql_error() there.

            Comment

            • porneL

              #7
              Re: Is this correct OO implementation

              On 21 Dec 2004 18:12:33 -0800, Jibbs <ryan.fairchild @gmail.com> wrote:
              [color=blue]
              > <?php
              > class sqlException extends Exception{
              > function __construct($ex ception){
              > parent::Excepti on($exception);
              > }
              > }[/color]

              parent::__const ruct();

              [color=blue]
              > Better?[/color]

              Much better IMO.

              When looking for PHP5 docs I keep running into:
              Nasz ranking na 2026 rok wskaże Ci najlepszych bukmacherów w Polsce. 🇵🇱 Korzystaj z bonusów do 2250 PLN, obstawiaj CS2 i Dota 2, i ciesz się wysokimi kursami. 💰

              which shows pretty nice DB model in PHP5.

              --
              porneL

              Comment

              • Paul Hanchett

                #8
                Re: Is this correct OO implementation

                I'll add a comment too--

                You should store the connection reference as well, and pass it in to all your database functions. What would happen if you instantiated two or more objects of this class? If you're willing to accept that you can only create a single object, then it should also be a singleton.

                --
                Paul Hanchett


                "CJ Llewellyn" <invalid@exampl e.con> wrote in message news:cqcrbc$quv $1@slavica.ukpo st.com...
                "Jibbs" <ryan.fairchild @gmail.com> wrote in message
                news:1103681553 .030771.196410@ f14g2000cwb.goo glegroups.com.. .[color=blue]
                > <?php
                > class sqlException extends Exception{
                > function __construct($ex ception){
                > parent::Excepti on($exception);
                > }
                > }
                >
                > class sqlConnect{
                >
                > private $username;
                > private $password;
                > private $db;
                > private $server;
                > private $link;
                >
                >
                > //contruct
                > function __construct($us ername,$passwor d,$server,$db){
                > $this->username = $username;
                > $this->password = $password;
                > $this->db = $db;
                > $this->server = $server;
                > $this->link =
                > @mysql_connect( $this->server,$this->username,$th is->password)
                >
                > if(!$this->link){
                > throw new sqlException(my sql_error());
                > }
                >
                > if(!@mysql_sele ct_db($this->db){
                > throw new sqlException(my sql_error());
                > }
                >
                > }
                >
                > //destruct
                > function __destruct(){
                > mysql_close($th is->link);
                > }
                >
                > ?>
                >
                > Better?[/color]

                The proof is always in the pudding, so try writing a class that deals with
                the ODBC api and see if your code can be cleanly ported (I know it cannot,
                your constructor parameters for instance will not be applicable for an ODBC
                connection).

                Your design should/could use one of three techniques in order to initialise
                the connection.

                1) embed the connection parameters into constants and set them inside the
                class file or a common configuration file. The constructor then just uses
                the parameters that are relevant to it's own implementation.

                2) pass the parameters to the constructor in an array.

                3) get the constructor to read a configuration file either in XML or ini
                format.

                Is there any value in keeping the connection parameters in the object once
                connection has been established? I can think of excellent reasons why the
                username/password should be discarded.

                Your initial design assumes that the user may wish to manually set the
                parameters before attempting connection. Your subsiquent decision to connect
                in the constructor invalidates this assumption. Therefore you might want to
                hand control back to the application, not the class.






                Comment

                • Jibbs

                  #9
                  Re: Is this correct OO implementation

                  Hey thanks for the tip. I have been reading on some more OO design and
                  I have a much better approach at this.

                  I have a huge problem right now, I am trying to implement Iterator
                  into my Mysql_Result class, but I can't find a good reference on how to
                  use it.

                  Comment

                  • Michael Fesser

                    #10
                    Re: Is this correct OO implementation

                    .oO(Jibbs)
                    [color=blue]
                    >I have a huge problem right now, I am trying to implement Iterator
                    >into my Mysql_Result class, but I can't find a good reference on how to
                    >use it.[/color]

                    Do you just have problems with the implementation of the required
                    methods or do you not even know where to start? The PHP manual gives at
                    least some basic informations about the iteration interfaces.

                    Object Iteration


                    Micha

                    Comment

                    Working...