SitePoint Sponsor |
|
User Tag List
Results 1 to 3 of 3
-
May 1, 2007, 03:30 #1
accessing a class from within a class
how do I access a class from within a class?
I'll give an example that wont work, but it's what I want to do
PHP Code:class db{
function connect(){
//connect to db
}
function query(){ //query }
...
}
$Db = new db();
$Db->connect();
class wut{
function whatever(){
$Db->query($sql);
}}
$woot = new wut();
$woot->wut();
I've not tried it, but could I use db::query($sql) in the second class?
but, wouldn't I have to connect to the database via db::connect again since it's static or w/e?
edit
would doing something like
PHP Code://in class two
var $h;
$h->query($sql);
//instead of just $woot->wut();
$woot->h = $Db;
$woot->wut();
my schools blocked my host, so I can't find out
-
May 1, 2007, 05:02 #2
- Join Date
- Jun 2004
- Location
- Copenhagen, Denmark
- Posts
- 6,157
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
A class is not an object. A class is an abstract type. An object is a concrete instance of a class. You can't access a class -- you access an object.
Try this:
PHP Code:class DB
{
function connect() {
//connect to db
}
function query() {
//query
}
}
class Wut
{
var $db;
function Wut($db) {
$this->db = $db;
}
function whatever() {
$this->db->query($sql);
}
}
$db = new DB();
$db->connect();
$woot = new Wut($db);
$woot->wut();
-
May 1, 2007, 06:15 #3
that worked
thanks
Bookmarks