Thread: OOP Help

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    OOP Help


    Hi everyone

    I have just started learning php5 OOP and am having trouble understanding when to create a new class in a new file.
    For example i have created database connection class with a method for connecting to a database.
    However i now want to have a method that runs a mysql_query using this connection. should i create this method in the same class? or create a new class for it.
    I also want to lead this on to creating methods for running mysql_fech_row functions ect.
    What is the best way to code for this using OOP?
    Sorry this is probably a stupid question.
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,614
    Rep Power
    1945
    For database connection, you should look into [phpnet=PDO]PDO[/phpnet] and its prepared statement.

    mysql_query has been deprecated with the newest version of PHP.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Location
    Auckland
    Posts
    93
    Rep Power
    24
    you may create a separate class which holds all database related methods ;which holds a static $link variable.

    alternatively, you can use PEAR libraries like PDO which already written to support all databases with abstraction layer.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    194
    Rep Power
    76
    Originally Posted by codejunkie75
    Hi everyone

    I have just started learning php5 OOP and am having trouble understanding when to create a new class in a new file.
    For example i have created database connection class with a method for connecting to a database.
    However i now want to have a method that runs a mysql_query using this connection. should i create this method in the same class? or create a new class for it.
    I also want to lead this on to creating methods for running mysql_fech_row functions ect.
    What is the best way to code for this using OOP?
    Sorry this is probably a stupid question.
    In your db class, you should start the connection to the database using whatever type of connection you want to use (mysql,mysqli,pdo) then have a main function in there called query or whatever that when called handles all of the query stuff and only requires a string containing the command for the query and the function should return the result in an array or just false if it failed or an error message
    so it should look like: query("SELECT * FROM table WHERE row=item AND id=1"); and if it succeeded it should return the results as an array

    so if you got this working correctly, all you would need to do is include your db class and call the query function whenever you want to query the database
  8. #5
  9. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    I like to think of data source interfaces.

    An interface is a concept where a selection of different classes all have the same method names (an external interface), so you could have one for a database, one for a file system, one for a web service, etc.

    When considering data from any source, there are 4 basic operations

    Create
    Read
    Update
    Delete

    (These are known colloquially as CRUD methods).

    Going back to your database class, I would have a mysql class which extends a generic data access class which implements a data access interface.

    All that is just fluff, in terms of code I'd connect in the constructor, then have a method for each C,R,U,D and maybe have two read methods - one for list type queries and one for full records; depends on your app.

    How that connection is done is up to you - you may connect with PDO and continue to use the PDO methods throughout the class or you may end up writing your own connection class elsewhere and contain in that the execute methods....But, IMO, that's what PDO already is and does not need much help
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    9
    Rep Power
    0

    Thanks


    Thanks guys much appreciated. Much clearer on what im doing now.
    I will check out PDO. what are the main advantages of using it?
    i am worried that if i start to learning/using it there may be restrictions.
    Also is PDO the common way of dealing with database connections or is it personal preference?
  12. #7
  13. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    Originally Posted by codejunkie75
    Thanks guys much appreciated. Much clearer on what im doing now.
    I will check out PDO. what are the main advantages of using it?
    i am worried that if i start to learning/using it there may be restrictions.
    Also is PDO the common way of dealing with database connections or is it personal preference?
    The recommended courses are either to go mysqli_* or PDO. Some prefer PDO because it's an interface capable of tapping into multiple database engines (kind of like Northie described above, I think), whereas the mysqli library is just a more modern version of the mysql library.
  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Location
    Burb of Detroit, Michigan
    Posts
    86
    Rep Power
    76
    From what I have read about mysqli -vs PDO is with mysqli being native to MySQL it's supposedly more configurable. However, PDO's advantage is that it's transferable to other kinds of databases and I also like to add that I like the ability in PDO to name the bind parameters (:username for example) than a ? mark as a placeholder. In my opinion it can get a little confusing if you have multiple ?? marks.
  16. #9
  17. Mad Scientist
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2007
    Location
    North Yorkshire, UK
    Posts
    3,661
    Rep Power
    4123
    I've never used mysqli but with pdo you can use either named parameters (eg :username) or ?

    These prepared statements are actually native to the database vendor (eg MySQL) and not something made up by the php developers. However, by default php emulates prepared statements, but this can be over ridden with options passed to pdo when you connect.

    As far as I am aware all the internals of php for mysql, mysqli and pdo use the native mysql client C library (although the mysql php extendion might use an old mysql c lib, hense phps deprecation of it)
    I said I didn't like ORM!!! <?php $this->model->update($this->request->resources[0])->set($this->request->getData())->getData('count'); ?>

    PDO vs mysql_* functions: Find a Migration Guide Here

    [ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]
  18. #10
  19. No Profile Picture
    Lost in code
    Devshed Supreme Being (6500+ posts)

    Join Date
    Dec 2004
    Posts
    8,317
    Rep Power
    7170
    As far as I am aware all the internals of php for mysql, mysqli and pdo use the native mysql client C library (although the mysql php extendion might use an old mysql c lib, hense phps deprecation of it)
    They used to wrap the mysql client library, however in newer versions of PHP the wrapper for the standard mysql client library has been replaced with a native php+mysql library written specifically and only for PHP. All three APIs can use either underlying C library. The php+mysql library is faster and has more features, but you can't take advantage of the extra features using the mysql API and you can only take advantage of some of the extra features using PDO.

    Comments on this post

    • richpri agrees : Thanks for the clarification.
    PHP FAQ

    Originally Posted by Spad
    Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around
  20. #11
  21. Web Developer/Musician
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Nov 2004
    Location
    Tennessee Mountains
    Posts
    2,408
    Rep Power
    1031
    ingnoring for the moment that you used an example of a database class. In general think of a class as a template for a real life object. Look at how other popular OOP based libraries break up functionality. A company has cutomers which have addresses and phone numbers and they do specific things for example. All of those could be broken up into separate classes. That is the whole idea of using objects.

    For instance, If a customer has two phone numbers (home and cell) you might decide to break those out into separate classes or store them in an array in a property depending on whether you want to be able to do special processing of the phone numbers for display and storage. If you want to do something special with the phone numbers make them a class with methods that do those things and then store them in the customer class in a property.

    With regard to databases, you are interfacing with an external software product. Different "brands" of which exist, all with their own set of capabilities and interfaces with some overlap. If you want to build versitality into the system you may have a connection class that uses multiple "adaptor" classes for different databases. You may also consider a database result as a separate object with it's own methods and properties.

IMN logo majestic logo threadwatch logo seochat tools logo