Solved

Good use of a class?

Posted on 2004-10-21
182 Views
Last Modified: 2006-11-17
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;
$conn->go("localhost","*****","*****","*****");


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.
0
Question by:MattMeister
    6 Comments
     
    LVL 48

    Assisted Solution

    by:hernst42
    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.
    0
     
    LVL 49

    Assisted Solution

    by:Roonaan
    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.

    -r-
    0
     

    Author Comment

    by:MattMeister
    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...
    0
     
    LVL 4

    Accepted Solution

    by:
    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");

    $page->output_head();

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

    $page->output_foot();

    ===============================

    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");

    $page->output_head();

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

    $page->output_foot();

    =============================

    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.

    Karl
    0
     
    LVL 4

    Expert Comment

    by:KarlPurkhardt
    i forgot to give you the link for the adodb library, so here it is if you want it...
    http://adodb.sourceforge.net/

    Its a great library, i recommend giving it a look.
    0
     

    Author Comment

    by:MattMeister
    Thanks to all
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Lean Six Sigma Project Manager Certification

    There are many schools of thought around successful project management, but few as highly regarded as the Six Sigma and Lean methods. With 37 hours of learning, this training will explain concrete processes for increasing efficiency and limiting wasted time and effort.

    Suggested Solutions

    Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
    Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    856 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now