By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,903 Members | 2,075 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,903 IT Pros & Developers. It's quick & easy.

class dependence

P: n/a
I have created a few classes which depend on other classes but I'm unsure
how to go about including the classes so they are visible to each other.

Can I just require_once at some point before use and make sure the order is
correct? or can I insert the require_once in the class itself(which then I'm
worried about forcing the location)? How does one normally go about this?

Thanks,
Jon
May 29 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On May 29, 3:54 pm, "Jon Slaughter" <Jon_Slaugh...@Hotmail.comwrote:
I have created a few classes which depend on other classes but I'm unsure
how to go about including the classes so they are visible to each other.

Can I just require_once at some point before use and make sure the order is
correct? or can I insert the require_once in the class itself(which then I'm
worried about forcing the location)? How does one normally go about this?

Thanks,
Jon
Use __autoload() -- it'll make your life a lot easier:

<http://www.php.net/autoload>

Here's what mine looks like (my files are named after my classes, so a
class called Foo lives in a file called Foo.php):

if(!function_exists('__autoload')) {
function __autoload($class_name) {
require_once($class_name . '.php');
}
}

ini_set('unserialize_callback_func', '__autoload');
Put all that in a file (call it autoload.php or something), then just
require_once that file on all your pages. No need to include each
class separately -- only things that are needed will be loaded (and in
the right order, too). Problem solved!

May 29 '07 #2

P: n/a

"ZeldorBlat" <ze********@gmail.comwrote in message
news:11*********************@k79g2000hse.googlegro ups.com...
On May 29, 3:54 pm, "Jon Slaughter" <Jon_Slaugh...@Hotmail.comwrote:
>I have created a few classes which depend on other classes but I'm unsure
how to go about including the classes so they are visible to each other.

Can I just require_once at some point before use and make sure the order
is
correct? or can I insert the require_once in the class itself(which then
I'm
worried about forcing the location)? How does one normally go about
this?

Thanks,
Jon

Use __autoload() -- it'll make your life a lot easier:

<http://www.php.net/autoload>

Here's what mine looks like (my files are named after my classes, so a
class called Foo lives in a file called Foo.php):

if(!function_exists('__autoload')) {
function __autoload($class_name) {
require_once($class_name . '.php');
}
}

ini_set('unserialize_callback_func', '__autoload');
Put all that in a file (call it autoload.php or something), then just
require_once that file on all your pages. No need to include each
class separately -- only things that are needed will be loaded (and in
the right order, too). Problem solved!
Well, that is essentially what I'm doing. Since I have a central php file
that handles everything I can just require it in that and it will act the
same... the problem is that the order has to be right and so the dependence
could get screwed up ;/ Was trying to avoid it if possible.

I might try that though,

Thanks,
Jon
May 29 '07 #3

P: n/a
On May 29, 5:23 pm, "Jon Slaughter" <Jon_Slaugh...@Hotmail.comwrote:
"ZeldorBlat" <zeldorb...@gmail.comwrote in message

news:11*********************@k79g2000hse.googlegro ups.com...
On May 29, 3:54 pm, "Jon Slaughter" <Jon_Slaugh...@Hotmail.comwrote:
I have created a few classes which depend on other classes but I'm unsure
how to go about including the classes so they are visible to each other.
Can I just require_once at some point before use and make sure the order
is
correct? or can I insert the require_once in the class itself(which then
I'm
worried about forcing the location)? How does one normally go about
this?
Thanks,
Jon
Use __autoload() -- it'll make your life a lot easier:
<http://www.php.net/autoload>
Here's what mine looks like (my files are named after my classes, so a
class called Foo lives in a file called Foo.php):
if(!function_exists('__autoload')) {
function __autoload($class_name) {
require_once($class_name . '.php');
}
}
ini_set('unserialize_callback_func', '__autoload');
Put all that in a file (call it autoload.php or something), then just
require_once that file on all your pages. No need to include each
class separately -- only things that are needed will be loaded (and in
the right order, too). Problem solved!

Well, that is essentially what I'm doing. Since I have a central php file
that handles everything I can just require it in that and it will act the
same... the problem is that the order has to be right and so the dependence
could get screwed up ;/ Was trying to avoid it if possible.

I might try that though,

Thanks,
Jon
When you use __autoload you don't need to worry about the order in
which you include your classes -- it all happens automatically. As
long as you include it before you use any classes it shouldn't be a
problem.

May 30 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.