Page 1 of 1

PHP Classes

Posted: Tue Sep 14, 2004 3:07 pm
by AndrewBacca
Hello,

I can using classes etc etc

but I want to know if its possible to call a class from inside a class

I'll explain why

I have a class that access a MySQL database (Item) and I have another class that does all the accessing (contect, dis-connect, etc) (db). Now I would love to bet able to us my Database (db) inside my Items (Item) classes as the db class counts queries, etc.

If this aint possible It's working as I am doing it but I thought I would ask :)

Cheers

Andrew [/b]

Posted: Tue Sep 14, 2004 3:09 pm
by feyd
look down the page a tiny bit :P: viewtopic.php?t=26004

Posted: Tue Sep 14, 2004 3:11 pm
by AndrewBacca
ahh sorry, i normally search

BUT i forgot to say they are in 2 different files can it still be done

Posted: Tue Sep 14, 2004 3:14 pm
by feyd
yes.

Posted: Tue Sep 14, 2004 3:15 pm
by AndrewBacca
anyidea how!? :P

ive tried includes an requires I just get an error

Posted: Tue Sep 14, 2004 3:18 pm
by feyd
I would bet you are missing a 'global' in your 'Item' methods..

post your code.

remove the image in your signature, it's 120K.

Posted: Tue Sep 14, 2004 3:19 pm
by AndrewBacca
I can tell you with out posting the code that I dont have one of them, I've seen them and never un-stood the purpose

so if i was to include my db class then do

global $db;

in the constructor?
that should work

Posted: Tue Sep 14, 2004 3:22 pm
by feyd
you need global $db in every method that uses the $db object.. alternately, you'd need a local reference to $db as a class property.

Posted: Tue Sep 14, 2004 3:27 pm
by AndrewBacca
Cheers got it :D

*sorry for the sig :)

Andrew