Best way to integrate classes

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

    Best way to integrate classes

    Hi all.

    I'm working here on a small project of mine. I'm not new to programming, but
    I'm new to PHP. You have to understand that I'm coming from C++, OOP world,
    so my code might seems a little too "object"-ified.

    Anyways, I've created a wrapper class for MySQL connectivity. It's barebones
    for now, no error checking or anything. So now I'm trying to create
    user/session-handling class that would work with the data stored in the
    database via DB class.

    What is the best approach to integrate the two classes? How would you do it?

    I see three possible solutions:


    1.) Have the Session class extend the DB class?
    class Session extends DB {
    // login();
    // logout();
    // etc...
    }

    This doesn't really make sense to me in terms of logical iheritance. Session
    is not really a DB-handling class. Besides, I'm unclear on a few things
    here. Will the constructor for DB class be called automatically and create a
    connection (it should in my mind) or should I be re-initializing all
    inherited vars?


    2.) First, instantiate an object of DB:
    $oDB = new DB();

    Then use it via global declaration in Session class

    class Session {
    global $oDB;

    // login();
    // logout();
    // etc...
    }

    Not the prettiest way, but it works. It would also allow to reuse the same
    MySQL connection via $oDB object further in the code.


    3.) Instantiate DB object as part of Session's vars:

    class Session {
    $_oDB = new DB();

    // login();
    // logout();
    // etc...
    }

    The only drawback I can see is that I would be unable to reuse the MySQL
    connection outside of Session class. I'd have to create a totaly new DB
    object.



    Oh and here's the DB class.

    // +++++++++++++++ ++++ DB CLASS +++++++++++++++ ++
    class DB {

    // public variables
    var $sServer;
    var $sPort;
    var $sUser;
    var $sPass;
    var $sDatabase;

    // private variables
    var $_link_id;
    var $_result_id;
    var $_last_query;

    // Constructor, uses db_connect()
    function DB ($user, $pass, $database, $server='localh ost', $port='3306') {
    $this->connect($use r, $pass, $database, $server, $port);
    }

    // Opens a connection to a db-server and selects a db
    function connect ($user, $pass, $database, $servet, $port) {
    $this->sServer = $server;
    $this->sPort = $port;
    $this->sUser = $user;
    $this->sPass = $pass;
    $this->sDatabase = $database;

    $this->_link_id = mysql_connect(" $server:$port", $user, $pass);

    $this->select_db($dat abase, $this->_link_id);
    }

    // Closes connection to the database
    function disconnect () {
    $this->free_result( );
    mysql_close($th is->_link_id);
    unset ($this->_link_id, $this->_result_id);
    }

    // Changes database in use
    function select_db ($database) {
    mysql_select_db ($this->sDatabase, $this->_link_id);
    }

    // Run db query
    function query ($query) {
    $this->_last_query = $query;
    $this->_result_id = mysql_query($qu ery, $this->_link_id);
    }

    // Frees up the memory required to store last query's results
    function free_result () {
    mysql_free_resu lt($this->_result_id);
    unset ($this->_result_id);
    }

    // Returns a single record array from the query results
    function fetch_row () {
    return mysql_fetch_row ($this->_result_id);
    }

    // Returns a single record as associative array from query results
    function fetch_assoc () {
    return mysql_fetch_ass oc($this->_result_id);
    }

    // Returns a single record as object from query results
    function fetch_object () {
    return mysql_fetch_obj ect($this->_result_id);
    }

    // Returns number of records found after SELECT statement
    function num_rows () {
    return mysql_num_rows( $this->_result_id);
    }

    // Return number of rows affected from INSERT, UPDATE, DELETE statements
    function affected_rows () {
    return mysql_affected_ rows($this->_link_id);
    }

    // Returns current db link_id
    function get_link_id () {
    return $this->_link_id;
    }

    // Return current db result_id
    function get_result_id () {
    return $this->_result_id;
    }

    // Return last query ran
    function get_last_query () {
    return $this->_last_query;
    }
    // -----------------------------------------------------------------------
    }

    Any comments are welcome, perhaps there're other way to do this that I'm not
    seeing.

    --
    Swartz


  • Mike Peters

    #2
    Re: Best way to integrate classes

    On 2004-03-06, Swartz wrote:[color=blue]
    > Hi all.
    >
    > I'm working here on a small project of mine. I'm not new to programming, but
    > I'm new to PHP. You have to understand that I'm coming from C++, OOP world,
    > so my code might seems a little too "object"-ified.
    >
    > Anyways, I've created a wrapper class for MySQL connectivity. It's barebones
    > for now, no error checking or anything. So now I'm trying to create
    > user/session-handling class that would work with the data stored in the
    > database via DB class.
    >
    > What is the best approach to integrate the two classes? How would you do it?
    >
    > I see three possible solutions:
    >
    >
    > 1.) Have the Session class extend the DB class?
    > class Session extends DB {
    > // login();
    > // logout();
    > // etc...
    > }
    >
    > This doesn't really make sense to me in terms of logical iheritance. Session
    > is not really a DB-handling class. Besides, I'm unclear on a few things
    > here. Will the constructor for DB class be called automatically and create a
    > connection (it should in my mind) or should I be re-initializing all
    > inherited vars?
    >
    >
    > 2.) First, instantiate an object of DB:
    > $oDB = new DB();
    >
    > Then use it via global declaration in Session class
    >
    > class Session {
    > global $oDB;
    >
    > // login();
    > // logout();
    > // etc...
    > }
    >
    > Not the prettiest way, but it works. It would also allow to reuse the same
    > MySQL connection via $oDB object further in the code.
    >
    >
    > 3.) Instantiate DB object as part of Session's vars:
    >
    > class Session {
    > $_oDB = new DB();
    >
    > // login();
    > // logout();
    > // etc...
    > }
    >
    > The only drawback I can see is that I would be unable to reuse the MySQL
    > connection outside of Session class. I'd have to create a totaly new DB
    > object.
    >[/color]
    I'd go route 3. You can still access the session's DB object:

    $my_session = new Session ();

    $sql = 'SELECT blah FROM blah';

    $my_session -> _oDB -> db_query ( $sql);

    HTH
    --
    Mike Peters
    mike [-AT-] ice2o [-DOT-] com
    I am a DevOps and Cloud architecture consultant based in Northumberland in the UK. I provide consultancy on DevOps and private and public cloud solutions. I design and implement continous integration and continuous delivery solutions using Open Source, bespoke and commercial off the shelf software. I can also provide training and tuition for you or your team in DevOps best practices, tooling and solutions, OpenSource software, automation and cloud architecture solutions.

    Comment

    • jn

      #3
      Re: Best way to integrate classes

      "Mike Peters" <o0__mike__0oRE MOVE@THIShotmai l.com> wrote in message
      news:88c3bebee1 10fa82039cd093a 8ff742d@news.te ranews.com...[color=blue]
      > On 2004-03-06, Swartz wrote:[color=green]
      > > Hi all.
      > >
      > > I'm working here on a small project of mine. I'm not new to programming,[/color][/color]
      but[color=blue][color=green]
      > > I'm new to PHP. You have to understand that I'm coming from C++, OOP[/color][/color]
      world,[color=blue][color=green]
      > > so my code might seems a little too "object"-ified.
      > >
      > > Anyways, I've created a wrapper class for MySQL connectivity. It's[/color][/color]
      barebones[color=blue][color=green]
      > > for now, no error checking or anything. So now I'm trying to create
      > > user/session-handling class that would work with the data stored in the
      > > database via DB class.
      > >
      > > What is the best approach to integrate the two classes? How would you do[/color][/color]
      it?[color=blue][color=green]
      > >
      > > I see three possible solutions:
      > >
      > >
      > > 1.) Have the Session class extend the DB class?
      > > class Session extends DB {
      > > // login();
      > > // logout();
      > > // etc...
      > > }
      > >
      > > This doesn't really make sense to me in terms of logical iheritance.[/color][/color]
      Session[color=blue][color=green]
      > > is not really a DB-handling class. Besides, I'm unclear on a few things
      > > here. Will the constructor for DB class be called automatically and[/color][/color]
      create a[color=blue][color=green]
      > > connection (it should in my mind) or should I be re-initializing all
      > > inherited vars?
      > >
      > >
      > > 2.) First, instantiate an object of DB:
      > > $oDB = new DB();
      > >
      > > Then use it via global declaration in Session class
      > >
      > > class Session {
      > > global $oDB;
      > >
      > > // login();
      > > // logout();
      > > // etc...
      > > }
      > >
      > > Not the prettiest way, but it works. It would also allow to reuse the[/color][/color]
      same[color=blue][color=green]
      > > MySQL connection via $oDB object further in the code.
      > >
      > >
      > > 3.) Instantiate DB object as part of Session's vars:
      > >
      > > class Session {
      > > $_oDB = new DB();
      > >
      > > // login();
      > > // logout();
      > > // etc...
      > > }
      > >
      > > The only drawback I can see is that I would be unable to reuse the MySQL
      > > connection outside of Session class. I'd have to create a totaly new DB
      > > object.
      > >[/color]
      > I'd go route 3. You can still access the session's DB object:
      >
      > $my_session = new Session ();
      >
      > $sql = 'SELECT blah FROM blah';
      >
      > $my_session -> _oDB -> db_query ( $sql);
      >
      > HTH
      > --
      > Mike Peters
      > mike [-AT-] ice2o [-DOT-] com
      > http://www.ice2o.com
      >[/color]

      That's not a good practice though is it? Wasn't that object supposed to be
      private to the session class?

      By the way, I'm no expert on OOP :)


      Comment

      • Mike Peters

        #4
        Re: Best way to integrate classes

        On 2004-03-06, jn wrote:[color=blue]
        > "Mike Peters" <o0__mike__0oRE MOVE@THIShotmai l.com> wrote in message
        > news:88c3bebee1 10fa82039cd093a 8ff742d@news.te ranews.com...[color=green]
        >> On 2004-03-06, Swartz wrote:[color=darkred]
        >> > Hi all.
        >> >
        >> > I'm working here on a small project of mine. I'm not new to programming,[/color][/color]
        > but[color=green][color=darkred]
        >> > I'm new to PHP. You have to understand that I'm coming from C++, OOP[/color][/color]
        > world,[color=green][color=darkred]
        >> > so my code might seems a little too "object"-ified.
        >> >
        >> > Anyways, I've created a wrapper class for MySQL connectivity. It's[/color][/color]
        > barebones[color=green][color=darkred]
        >> > for now, no error checking or anything. So now I'm trying to create
        >> > user/session-handling class that would work with the data stored in the
        >> > database via DB class.
        >> >
        >> > What is the best approach to integrate the two classes? How would you do[/color][/color]
        > it?[color=green][color=darkred]
        >> >
        >> > I see three possible solutions:
        >> >
        >> >
        >> > 1.) Have the Session class extend the DB class?
        >> > class Session extends DB {
        >> > // login();
        >> > // logout();
        >> > // etc...
        >> > }
        >> >
        >> > This doesn't really make sense to me in terms of logical iheritance.[/color][/color]
        > Session[color=green][color=darkred]
        >> > is not really a DB-handling class. Besides, I'm unclear on a few things
        >> > here. Will the constructor for DB class be called automatically and[/color][/color]
        > create a[color=green][color=darkred]
        >> > connection (it should in my mind) or should I be re-initializing all
        >> > inherited vars?
        >> >
        >> >
        >> > 2.) First, instantiate an object of DB:
        >> > $oDB = new DB();
        >> >
        >> > Then use it via global declaration in Session class
        >> >
        >> > class Session {
        >> > global $oDB;
        >> >
        >> > // login();
        >> > // logout();
        >> > // etc...
        >> > }
        >> >
        >> > Not the prettiest way, but it works. It would also allow to reuse the[/color][/color]
        > same[color=green][color=darkred]
        >> > MySQL connection via $oDB object further in the code.
        >> >
        >> >
        >> > 3.) Instantiate DB object as part of Session's vars:
        >> >
        >> > class Session {
        >> > $_oDB = new DB();
        >> >
        >> > // login();
        >> > // logout();
        >> > // etc...
        >> > }
        >> >
        >> > The only drawback I can see is that I would be unable to reuse the MySQL
        >> > connection outside of Session class. I'd have to create a totaly new DB
        >> > object.
        >> >[/color]
        >> I'd go route 3. You can still access the session's DB object:
        >>
        >> $my_session = new Session ();
        >>
        >> $sql = 'SELECT blah FROM blah';
        >>
        >> $my_session -> _oDB -> db_query ( $sql);
        >>
        >> HTH
        >> --
        >> Mike Peters
        >> mike [-AT-] ice2o [-DOT-] com
        >> http://www.ice2o.com
        >>[/color]
        >
        > That's not a good practice though is it? Wasn't that object supposed to be
        > private to the session class?
        >
        > By the way, I'm no expert on OOP :)
        >[/color]
        PHP4 has no concept of public / private, maybe the OP is using PHP5 but he
        didn't say so. Anyway this is merely an example of containment - the
        Session object 'has a' database object. We can access it directly like
        above (as if we'd declared it public) or we could write a 'public' method
        for our session class to access the 'private' object, the end result is the
        same. I can see your point, and, in a compiled language I would agree, but
        we're using php here so we also need to think about performance and the extra
        overhead it would take to do it the 'right' way, at least in PHP4 where
        the object model is less than perfect.
        --
        Mike Peters
        mike [-AT-] ice2o [-DOT-] com
        I am a DevOps and Cloud architecture consultant based in Northumberland in the UK. I provide consultancy on DevOps and private and public cloud solutions. I design and implement continous integration and continuous delivery solutions using Open Source, bespoke and commercial off the shelf software. I can also provide training and tuition for you or your team in DevOps best practices, tooling and solutions, OpenSource software, automation and cloud architecture solutions.

        Comment

        • Swartz

          #5
          Re: Best way to integrate classes

          Hi.
          [color=blue][color=green][color=darkred]
          > > > 3.) Instantiate DB object as part of Session's vars:
          > > >
          > > > class Session {
          > > > $_oDB = new DB();
          > > >
          > > > // login();
          > > > // logout();
          > > > // etc...
          > > > }[/color][/color][/color]

          I guess I better correct myself before somebody else does.
          Unfortunatelly u cant have dynamic created values assigned to a class
          variable.
          Expression $_oDB = new DB() is invalid.

          So the code should be the following:

          class Session {
          var $_oDB;

          function Session () {
          $this->_oDB = new DB();
          }
          // login, logout, etc

          }
          [color=blue][color=green]
          > > I'd go route 3. You can still access the session's DB object:
          > > $my_session = new Session ();
          > > $sql = 'SELECT blah FROM blah';
          > > $my_session -> _oDB -> db_query ( $sql);[/color]
          > That's not a good practice though is it? Wasn't that object supposed to be
          > private to the session class?[/color]
          Well, technically yes, it is not a good OOP. Mainly because class vars that
          start with an underscore "_" are private vars by convention that many PHP
          programmers use.

          --
          Swartz



          Comment

          • Mike Peters

            #6
            Re: Best way to integrate classes

            On 2004-03-07, Swartz wrote:[color=blue]
            > Hi.
            >[color=green][color=darkred]
            >> > > 3.) Instantiate DB object as part of Session's vars:
            >> > >
            >> > > class Session {
            >> > > $_oDB = new DB();
            >> > >
            >> > > // login();
            >> > > // logout();
            >> > > // etc...
            >> > > }[/color][/color]
            >
            > I guess I better correct myself before somebody else does.
            > Unfortunatelly u cant have dynamic created values assigned to a class
            > variable.
            > Expression $_oDB = new DB() is invalid.
            >
            > So the code should be the following:
            >
            > class Session {
            > var $_oDB;
            >
            > function Session () {
            > $this->_oDB = new DB();
            > }
            > // login, logout, etc
            >
            > }[/color]

            I'd assumed your original post was merely shorthand and you'd meant this
            anyway.
            [color=blue]
            >[color=green][color=darkred]
            >> > I'd go route 3. You can still access the session's DB object:
            >> > $my_session = new Session ();
            >> > $sql = 'SELECT blah FROM blah';
            >> > $my_session -> _oDB -> db_query ( $sql);[/color]
            >> That's not a good practice though is it? Wasn't that object supposed to be
            >> private to the session class?[/color]
            > Well, technically yes, it is not a good OOP. Mainly because class vars that
            > start with an underscore "_" are private vars by convention that many PHP
            > programmers use.
            >
            > --
            > Swartz
            >
            >
            >[/color]


            --
            Mike Peters
            mike [-AT-] ice2o [-DOT-] com
            I am a DevOps and Cloud architecture consultant based in Northumberland in the UK. I provide consultancy on DevOps and private and public cloud solutions. I design and implement continous integration and continuous delivery solutions using Open Source, bespoke and commercial off the shelf software. I can also provide training and tuition for you or your team in DevOps best practices, tooling and solutions, OpenSource software, automation and cloud architecture solutions.

            Comment

            • Nikolai Chuvakhin

              #7
              Re: Best way to integrate classes

              "Swartz" <swartz@inbox.r u> wrote in message
              news:<104khdbgr afe251@corp.sup ernews.com>...[color=blue]
              >
              > I'm working here on a small project of mine. I'm not new to programming, but
              > I'm new to PHP. You have to understand that I'm coming from C++, OOP world,
              > so my code might seems a little too "object"-ified.[/color]

              Actually, it doesn't seem "a little too objectified"; it seems
              unnecessarily objectified. You are creating lots of overhead
              here...
              [color=blue]
              > What is the best approach to integrate the two classes?[/color]

              The best approach is not to have classes at all, unless you
              need them.
              [color=blue]
              > How would you do it?[/color]

              By writing standalone functions: login(), logout(), etc. Ask
              yourself a simple question: will you ever have more than one
              instance of your Session class running? Most likely, the answer
              to that question is no, so you will do just fine with standalone
              functions; they will get the job done and won't create any
              unnecessary overhead...
              [color=blue]
              > perhaps there're other way to do this that I'm not seeing.[/color]

              Of course. Try to unlearn OOP, if you can.

              Cheers,
              NC

              Comment

              • Hayden Kirk

                #8
                Re: Best way to integrate classes

                Worst advice ever... im sorry.

                I create classes for everything. Why? Because I might write a project in a
                few months, instead of starting from scratch I can just include those
                classes.
                [color=blue]
                > Of course. Try to unlearn OOP, if you can.[/color]

                Clearly you have never done much OOP.

                Wait for php 5 and things are going to change.


                "Nikolai Chuvakhin" <nc@iname.com > wrote in message
                news:32d7a63c.0 403061840.51a9b 9fa@posting.goo gle.com...[color=blue]
                > "Swartz" <swartz@inbox.r u> wrote in message
                > news:<104khdbgr afe251@corp.sup ernews.com>...[color=green]
                > >
                > > I'm working here on a small project of mine. I'm not new to programming,[/color][/color]
                but[color=blue][color=green]
                > > I'm new to PHP. You have to understand that I'm coming from C++, OOP[/color][/color]
                world,[color=blue][color=green]
                > > so my code might seems a little too "object"-ified.[/color]
                >
                > Actually, it doesn't seem "a little too objectified"; it seems
                > unnecessarily objectified. You are creating lots of overhead
                > here...
                >[color=green]
                > > What is the best approach to integrate the two classes?[/color]
                >
                > The best approach is not to have classes at all, unless you
                > need them.
                >[color=green]
                > > How would you do it?[/color]
                >
                > By writing standalone functions: login(), logout(), etc. Ask
                > yourself a simple question: will you ever have more than one
                > instance of your Session class running? Most likely, the answer
                > to that question is no, so you will do just fine with standalone
                > functions; they will get the job done and won't create any
                > unnecessary overhead...
                >[color=green]
                > > perhaps there're other way to do this that I'm not seeing.[/color]
                >
                > Of course. Try to unlearn OOP, if you can.
                >
                > Cheers,
                > NC[/color]


                Comment

                • Bruno Desthuilliers

                  #9
                  Re: Best way to integrate classes

                  Swartz wrote:[color=blue]
                  > Hi all.
                  >
                  > I'm working here on a small project of mine. I'm not new to programming, but
                  > I'm new to PHP. You have to understand that I'm coming from C++, OOP world,
                  > so my code might seems a little too "object"-ified.
                  >
                  > Anyways, I've created a wrapper class for MySQL connectivity. It's barebones
                  > for now, no error checking or anything. So now I'm trying to create
                  > user/session-handling class that would work with the data stored in the
                  > database via DB class.
                  >
                  > What is the best approach to integrate the two classes? How would you do it?
                  >
                  > I see three possible solutions:
                  >
                  >
                  > 1.) Have the Session class extend the DB class?[/color]
                  (snip inheritence)[color=blue]
                  >
                  > 2.) First, instantiate an object of DB:
                  > $oDB = new DB();
                  >
                  > Then use it via global declaration in Session class[/color]
                  (snip use of a global)[color=blue]
                  >
                  > 3.) Instantiate DB object as part of Session's vars:
                  >
                  > class Session {
                  > $_oDB = new DB();
                  >
                  > // login();
                  > // logout();
                  > // etc...
                  > }
                  >
                  > The only drawback I can see is that I would be unable to reuse the MySQL
                  > connection outside of Session class. I'd have to create a totaly new DB
                  > object.[/color]

                  And what about :

                  class Session
                  {
                  var $_dbObject;

                  function Session(&$dbObj ect)
                  {
                  $this->dbObject =& $dbObject;
                  }
                  /* ... */
                  }

                  $db = new DB();
                  $session = new Session($db);

                  Easy enough, no ? I'd thought that <quote>coming from C++, OOP
                  world</quote>, you would at least know such a basic OO construction ?-)

                  Bruno

                  Comment

                  • Bruno Desthuilliers

                    #10
                    Re: Best way to integrate classes

                    Nikolai Chuvakhin wrote:[color=blue]
                    > "Swartz" <swartz@inbox.r u> wrote in message
                    > news:<104khdbgr afe251@corp.sup ernews.com>...
                    >[color=green]
                    >>I'm working here on a small project of mine. I'm not new to programming, but
                    >>I'm new to PHP. You have to understand that I'm coming from C++, OOP world,
                    >>so my code might seems a little too "object"-ified.[/color]
                    >
                    > Actually, it doesn't seem "a little too objectified"; it seems
                    > unnecessarily objectified. You are creating lots of overhead
                    > here...
                    >[color=green]
                    >>What is the best approach to integrate the two classes?[/color]
                    >
                    > The best approach is not to have classes at all, unless you
                    > need them.
                    >[color=green]
                    >>How would you do it?[/color]
                    >
                    > By writing standalone functions: login(), logout(), etc. Ask
                    > yourself a simple question: will you ever have more than one
                    > instance of your Session class running? Most likely, the answer
                    > to that question is no, so you will do just fine with standalone
                    > functions; they will get the job done and won't create any
                    > unnecessary overhead...[/color]

                    All your rant against OO could be justified if PHP had any other support
                    for modularity. Since this is not the case, and there's no way to have
                    functions or vars existing on a 'module' scope only, the only way to
                    write clean PHP code is to use OO.

                    The "unnecessar y overhead" is the price to pay to have clean, modular
                    code with lo coupling, and as such is *never* "unnecessar y" except
                    perhaps for the most basic, throw-away scripts.


                    Bruno

                    Comment

                    • Tim Van Wassenhove

                      #11
                      Re: Best way to integrate classes

                      On 2004-03-06, Swartz <swartz@inbox.r u> wrote:[color=blue]
                      > Hi all.
                      >
                      > I'm working here on a small project of mine. I'm not new to programming, but
                      > I'm new to PHP. You have to understand that I'm coming from C++, OOP world,
                      > so my code might seems a little too "object"-ified.
                      >
                      > Anyways, I've created a wrapper class for MySQL connectivity. It's barebones
                      > for now, no error checking or anything. So now I'm trying to create
                      > user/session-handling class that would work with the data stored in the
                      > database via DB class.
                      >
                      > I see three possible solutions:
                      > 1.) Have the Session class extend the DB class?
                      > 2.) First, instantiate an object of DB:
                      > Then use it via global declaration in Session class
                      > 3.) Instantiate DB object as part of Session's vars:[/color]

                      I'd go for the 4th solution: An interface Session with functions login,
                      logout, etc...

                      Then implement this interface in a class DBSession and pass a reference
                      to the DB object as an argument in the constructor.

                      --

                      Comment

                      • Tony Marston

                        #12
                        Re: Best way to integrate classes


                        "Bruno Desthuilliers" <bdesth.quelque chose@free.quel quepart.fr> wrote in
                        message news:404ba4bc$0 $305$636a15ce@n ews.free.fr...[color=blue]
                        > Nikolai Chuvakhin wrote:[color=green]
                        > > "Swartz" <swartz@inbox.r u> wrote in message
                        > > news:<104khdbgr afe251@corp.sup ernews.com>...
                        > >[color=darkred]
                        > >>I'm working here on a small project of mine. I'm not new to programming,[/color][/color][/color]
                        but[color=blue][color=green][color=darkred]
                        > >>I'm new to PHP. You have to understand that I'm coming from C++, OOP[/color][/color][/color]
                        world,[color=blue][color=green][color=darkred]
                        > >>so my code might seems a little too "object"-ified.[/color]
                        > >
                        > > Actually, it doesn't seem "a little too objectified"; it seems
                        > > unnecessarily objectified. You are creating lots of overhead
                        > > here...
                        > >[color=darkred]
                        > >>What is the best approach to integrate the two classes?[/color]
                        > >
                        > > The best approach is not to have classes at all, unless you
                        > > need them.
                        > >[color=darkred]
                        > >>How would you do it?[/color]
                        > >
                        > > By writing standalone functions: login(), logout(), etc. Ask
                        > > yourself a simple question: will you ever have more than one
                        > > instance of your Session class running? Most likely, the answer
                        > > to that question is no, so you will do just fine with standalone
                        > > functions; they will get the job done and won't create any
                        > > unnecessary overhead...[/color]
                        >
                        > All your rant against OO could be justified if PHP had any other support
                        > for modularity. Since this is not the case, and there's no way to have
                        > functions or vars existing on a 'module' scope only, the only way to
                        > write clean PHP code is to use OO.[/color]

                        What is your definition of 'clean' code. It is possible to write beautiful
                        code without using OO, just as it is possible to write crap code with OO.
                        Using one technique or the other does not guarantee that your code will be
                        either excellent or excrement - it is not the technique, it is the
                        technician.

                        --
                        Tony Marston

                        This is Tony Marston's web site, containing personal information plus pages devoted to the Uniface 4GL development language, XML and XSL, PHP and MySQL, and a bit of COBOL




                        Comment

                        • R. Rajesh Jeba Anbiah

                          #13
                          Re: Best way to integrate classes

                          [Top-post fixed.]

                          "Hayden Kirk" <spam@spam.co m> wrote in message news:<eNw2c.344 85$ws.3457177@n ews02.tsnz.net> ...[color=blue]
                          > "Nikolai Chuvakhin" <nc@iname.com > wrote in message
                          > news:32d7a63c.0 403061840.51a9b 9fa@posting.goo gle.com...[color=green]
                          > > "Swartz" <swartz@inbox.r u> wrote in message
                          > > news:<104khdbgr afe251@corp.sup ernews.com>...[color=darkred]
                          > > >
                          > > > I'm working here on a small project of mine. I'm not new to programming,[/color][/color]
                          > but[color=green][color=darkred]
                          > > > I'm new to PHP. You have to understand that I'm coming from C++, OOP[/color][/color]
                          > world,[color=green][color=darkred]
                          > > > so my code might seems a little too "object"-ified.[/color]
                          > >
                          > > Actually, it doesn't seem "a little too objectified"; it seems
                          > > unnecessarily objectified. You are creating lots of overhead
                          > > here...
                          > >[color=darkred]
                          > > > What is the best approach to integrate the two classes?[/color]
                          > >
                          > > The best approach is not to have classes at all, unless you
                          > > need them.
                          > >[color=darkred]
                          > > > How would you do it?[/color]
                          > >
                          > > By writing standalone functions: login(), logout(), etc. Ask
                          > > yourself a simple question: will you ever have more than one
                          > > instance of your Session class running? Most likely, the answer
                          > > to that question is no, so you will do just fine with standalone
                          > > functions; they will get the job done and won't create any
                          > > unnecessary overhead...
                          > >[color=darkred]
                          > > > perhaps there're other way to do this that I'm not seeing.[/color]
                          > >[/color]
                          > Worst advice ever... im sorry.
                          >
                          > I create classes for everything. Why? Because I might write a project in a
                          > few months, instead of starting from scratch I can just include those
                          > classes.[/color]

                          I also use classes not for this reason; but as I like OOP. Nikolai
                          Chuvakhin may reuse his functions instead of classes.
                          [color=blue][color=green]
                          > > Of course. Try to unlearn OOP, if you can.[/color]
                          >
                          > Clearly you have never done much OOP.[/color]

                          Sounds like a newcomer? Nikolai Chuvakhin is one among many
                          anti-OOP regulars here :-)

                          --
                          "I don't believe in the God who doesn't give me food, but shows me
                          heaven!"--Swami Vivekanandha
                          Email: rrjanbiah-at-Y!com

                          Comment

                          • R. Rajesh Jeba Anbiah

                            #14
                            Re: Best way to integrate classes

                            "Swartz" <swartz@inbox.r u> wrote in message news:<104khdbgr afe251@corp.sup ernews.com>...[color=blue]
                            > What is the best approach to integrate the two classes? How would you do it?
                            > I see three possible solutions:[/color]

                            Indeed a nice question.
                            [color=blue]
                            > 1.) Have the Session class extend the DB class?[/color]

                            Unfortunately this style is bit common in PHP. IIRC, this is bit
                            common among people who use PEAR. I don't know, why people inherit the
                            classes that are not actually a super class. I often see this: PHP
                            programmers often extends a class as a "fix" to "add" some more
                            methods instead of viewing it as a super object.
                            [color=blue]
                            > 2.) First, instantiate an object of DB:
                            > $oDB = new DB();
                            > Then use it via global declaration in Session class
                            > class Session {
                            > global $oDB;[/color]

                            I'm alergic to global variables and using them inside the classes.
                            For me, an object should not be controlled by a public variable.
                            [color=blue]
                            > 3.) Instantiate DB object as part of Session's vars:
                            > class Session {
                            > $_oDB = new DB();[/color]

                            This is bit awkward.

                            I usually go for Bruno Desthuilliers's solution.

                            --
                            "I don't believe in the God who doesn't give me food, but shows me
                            heaven!"--Swami Vivekanandha
                            Email: rrjanbiah-at-Y!com

                            Comment

                            • R. Rajesh Jeba Anbiah

                              #15
                              Re: Best way to integrate classes

                              "Swartz" <swartz@inbox.r u> wrote in message news:<104khdbgr afe251@corp.sup ernews.com>...[color=blue]
                              > What is the best approach to integrate the two classes? How would you do it?
                              > I see three possible solutions:[/color]

                              Indeed a nice question.
                              [color=blue]
                              > 1.) Have the Session class extend the DB class?[/color]

                              Unfortunately this style is bit common in PHP. IIRC, this is bit
                              common among people who use PEAR. I don't know, why people inherit the
                              classes that are not actually a super class. I often see this: PHP
                              programmers often extends a class as a "fix" to "add" some more
                              methods instead of viewing it as a super object.

                              [color=blue]
                              > 2.) First, instantiate an object of DB:
                              > $oDB = new DB();
                              > Then use it via global declaration in Session class
                              > class Session {
                              > global $oDB;[/color]

                              I'm alergic to global variables and using them inside the classes.
                              For me, an object should not be controlled by a public variable.
                              [color=blue]
                              > 3.) Instantiate DB object as part of Session's vars:
                              > class Session {
                              > $_oDB = new DB();[/color]

                              This is bit awkward.

                              I usually go for Bruno Desthuilliers's solution.

                              --
                              "I don't believe in the God who doesn't give me food, but shows me
                              heaven!"--Swami Vivekanandha
                              Email: rrjanbiah-at-Y!com

                              Comment

                              Working...