Good use of a class?

class conn {
   function go($host,$user,$pass,$db) {
      $conn = mysql_connect($host,$user,$pass) or die("mySQL connect error");
      mysql_select_db($db,$conn) or die ("mySQL DB error");

$conn = new conn;

What i want to know is, is it worth connecting to a DB this way?
In this case would it make more sense to just directly call the functions mysql_connect and mysql_select_db?

If this is a logical use is there anything i can do to improve its functionality, i was thinking of writing it without the go function so i just had the class and two connect lines but wasn't sure if this would actually serve any benefit.

Thoughts please...

Points will be increased if my knowledge of OOP is expanded throughout this question.
Who is Participating?
FIrst off all i'd like to point your attention to a very nice database library called adodb, I use this for my database connections - its a really great library and its free to use for personal and commercial use (aslong as you dont ahere to the license).  Heres an example of using classes for creating pages.

class CPage {
  var $title;
  function CPage($title) {
    $this->title = $title;

the above class (albeit small and basic) is a base class for all pages - you could add more functionality to the base class, this would be things like the title (which ive added), basically your base clas would contain functionality and features that relate to all pages you create.  For example you could add a access level to check if the user is allowed to access the page.

Extending the base class to support a very basic access level check...

class CPage {
  var $title;
  var $access_level;
  function CPage($title, $access_level) {
    $this->title = $title;
    $this->access_level = $access_level;
  function allow_access($users_access_level) {
    if ($this->access_level >= $user_access_level) return true;
    return false;

So with the above code you could define a basis for each page i.e.


$users_access_level = 1;// this could be obtained from a cookie,
                                    // a database, or some other source i.e. a user class

$page = new CPage("My Title", 1);

if (!$page->allow_access($users_access_level))  die("You do not have access to this page");

// load page here...


However the above class is still very basic, an output_head method could be used to output the 'beginning' of the page (<html><head><title>Your specified Title</title></head><body>) using the title that you specified.

You could then add your 'page' specific code and create another function to output the 'foot' of the page (</body></html>)

You could then do something like


$page = new CPage("My Title", 1);

if (!$page->allow_access($users_access_level))  die("You do not have access to this page");


echo "This is the main section of the page, this is inserted inbetween the body tags";



So its now getting a little more functionality, but lets say we wanted to create a login page, and we want functions specific to this page (but we also want to access properties and methods from our CPage class).

Extending the CPage class....


class CLoginPage extends CPage {
  function CLogin($title) {
    parent::CPage($title, 0);// notice we bypass the user level, we do this because we know
                                       // that a login page does not require any special user proviledges
  // a simple login function
  function Login($username, $password) {
    // you would add your login code here, therefor wrapping it up nicely inside a method of
    // CLoginPage class.

with the login code inplace, you could create a login page like so...


$page = new CLoginPage("My Login Page");


if ($page->Login($_POST['username'], $_POST['password']))
  echo "You have been logged in":
  echo "Unable to login, please check username and password and try again";



Thats pretty much the basics of it, I hope its clear enough to undetstand.  You just keep on expanding your base class and extending it to create new pages with 'page specific functionality'.

Someone may be able to explain this better, but atleast i gave it a shot :)

anyway, hope it helps.

The benefit of using OOP is that if you change the underlying database of your application you only need to write anotherclass which can handle the connection to a oracle database. So your application runs on oracle by only changeing the db-access class. Else you need to add switches or if or need to replace all mysql_* function calls. Doing it via OOP makes your code shorter and better readable.

 But instead of writing your own DB-OO classes have a look at the OO-DB class of pear.

But you can do it if you only want to learn how OOP is done.
If you were to add query-ing functionality to your class, then it would be usefull indeed.

In the current state you could also just write a connect() function without the class.

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

MattMeisterAuthor Commented:
Thanks for the comments so far, what i am really using this for is a basis for learning where OOP can be used and the benefits it servers over standard functions etc. I now understand the theory of "object oriented" so i guess what i am looking for is some opinions and tips about good uses and practices with classes.

I guess maybe an example is the best way for me to learn so say if i had a class which needed to be fed two variables which needed to be processed by two functions thereafter. What would the bare bones structure look like in a well written class, firsrt creating the object, then defining the variables and then calling the functions...
i forgot to give you the link for the adodb library, so here it is if you want it...

Its a great library, i recommend giving it a look.
MattMeisterAuthor Commented:
Thanks to all
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.