Current location: Hot Scripts Forums » Programming Languages » PHP » OOP question


OOP question

Reply
  #1 (permalink)  
Old 07-29-06, 09:53 AM
landing's Avatar
landing landing is offline
Coding Addict
 
Join Date: Jul 2006
Location: Scotland
Posts: 302
Thanks: 0
Thanked 0 Times in 0 Posts
OOP question

I've just started teaching myself OOP in PHP.

I've created a database class, which I can use for connecting to the DB, selecting a DB, running queries etc.

My question is, how can I use this object within other objects? I have created a few other objects which contain functions for looking up records in the database. I want to be able to use my Database class to run the queries.


Any enlightenment would be greatly appreciated.

Thanks in advance
__________________
Always sanitise your data


Best regards
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 07-31-06, 01:04 AM
Patiek Patiek is offline
Wannabe Coder
 
Join Date: Nov 2003
Posts: 165
Thanks: 0
Thanked 0 Times in 0 Posts
There are a few ways (depending on class structure). However, your class is probably going to be using syntax similar to:

$DB = new YourDatabaseClass(...);
$DB->query("your query");


and so on from within your other classes. If you only need / have the one database or database object then you can go about passing in that database via constructor (a function within your class with same name as your class that is called when you class is initialized).

I am really not sure what else you are going after, so here is a basic example of what I am talking about (although you can approach this in many different ways). The one thing I would discourage is the use of globals (such as using "global $DB;" ). Also, the style below should be PHP 4 friendly.

PHP Code:

<?php


class YourFirstClass
{
    var 
$DB;
    var 
$someObject;

    function 
YourFirstClass()
    {
        
// initialize your database object
        
$this->DB = new YourDatabaseClass(...);

        
// initialize some other class
        
$this->someObject = new YourSecondClass($this->DB);
    }

    function 
doSomething()
    {
        
$this->DB->query("whatever");
        .....
    }
}


class 
YourSecondClass
{
    var 
$DB;

    function 
YourSecondClass(&$DB)
    {
        
// we do not want to replicate object, so we use &
        
$this->DB  =& $DB;
    }

    function 
doSomething()
    {
        
$this->DB->query("whatever else");
        ...
    }
}


class 
YourDatabaseClass
{
    ... 
whatever your database class has...
}
If you like I can also post a database class that I use, just ask.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 07-31-06, 03:29 AM
darksystem darksystem is offline
Newbie Coder
 
Join Date: Mar 2005
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
try also the inheritance functionality of OOP

e.g.
Your created class for your DB

class DB {

}


This class will inherits your class DB including its vars and methods or proporties.

class test extends DB
{

}

got it?

PS: I suggest your better read the Classes and Objects of the phpmanual.
You can download here
http://www.php.net/download-docs.php

Hope it helps.
__________________
Ebay API, OSC/CRE/OscMax/ZenCart/SEO Services
5MServices - Ebay Import Tool - CollectaMania - Email
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 07-31-06, 09:55 AM
landing's Avatar
landing landing is offline
Coding Addict
 
Join Date: Jul 2006
Location: Scotland
Posts: 302
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Patiek and darksystem. Very helpful!

OOP is a whole new world to me, but I'm eager to learn because I've already realised the benefits.

Time to start some serious reading...
__________________
Always sanitise your data


Best regards
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 07-31-06, 02:48 PM
duesi's Avatar
duesi duesi is offline
Wannabe Coder
 
Join Date: Jun 2006
Posts: 225
Thanks: 0
Thanked 0 Times in 0 Posts
And if you put your class in a different file you need to include it first:

PHP Code:

include_once("db.lib.php");


$db = new db();
$count $db->query("SELECT COUNT(*) FROM my_table");
... 
I keep all my classes in a separate file, this keeps my head free.
I think this is what OO is mainly about: you do not need to think about implementation details. $db provides a service (maipulating a DB), but you don't care about the inner workings of that service (at least after you have written the class).

Happy Coding!
__________________
Duesi

"One of the great skills in using any language is knowing what not to use, what not to say" (Ron Jeffries)

http://www.swissbytes.de

Last edited by duesi; 07-31-06 at 02:51 PM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Injecting a string into an If Statement ? nova912 PHP 4 07-21-06 03:04 PM
Download masking question... astrkalj Web Servers 0 07-09-06 05:53 PM
Posting a question / answer on site markcody PHP 2 11-23-04 02:58 PM
[PHP] Array question UmiSal Script Requests 1 04-05-04 02:52 PM
question and answer software jaydifox C/C++ 0 02-21-04 10:26 AM


All times are GMT -5. The time now is 11:37 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.