Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Good use of a class?

Posted on 2004-10-21
Medium Priority
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;

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.
Question by:MattMeister
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 48

Assisted Solution

hernst42 earned 240 total points
ID: 12373952
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.
LVL 49

Assisted Solution

Roonaan earned 160 total points
ID: 12378929
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.


Author Comment

ID: 12379141
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...
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Accepted Solution

KarlPurkhardt earned 600 total points
ID: 12379672
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.


Expert Comment

ID: 12379704
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.

Author Comment

ID: 12395822
Thanks to all

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses

618 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