473,379 Members | 1,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

class dependence

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
3 1473
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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Fernando Rodriguez | last post by:
Hi, I need to traverse the methods defined in a class and its superclasses. This is the code I'm using: # An instance of class B should be able to check all the methods defined in B #and A,...
1
by: Oplec | last post by:
Hi, I'm learning C++ as a hobby using The C++ Programming Language : Special Edition by Bjarne Stroustrup. I'm working on chpater 13 exercises that deal with templates. Exercise 13.9 asks for me...
1
by: Leslie | last post by:
I have 2 dlls and they reference each other, thus causing the circular dependence error. I have read about other cases and people say use Interfaces to solve the problem with the shared methods. ...
15
by: Mon | last post by:
I am in the process of reorganizing my code and came across and I came across a problem, as described in the subject line of this posting. I have many classes that have instances of other classes...
9
by: Banaticus Bart | last post by:
I wrote an abstract base class from which I've derived a few other classes. I'd like to create a base class array where each element is an instance of a derived object. I can create a base class...
5
by: Andy | last post by:
Hi all, I have a site with the following architecture: Common.Web.dll - Contains a CommonPageBase class which inherits System.Web.UI.Page myadd.dll - Contains PageBase which inherits...
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
22
by: Boris | last post by:
I'm porting code from Windows to UNIX and ran into a problem with dynamic_cast. Imagine a class hierarchy with three levels: class Level2 derives from Level1 which derives from Base. If you look...
6
by: kepeng | last post by:
There are 2 abstract base classes: class IB; class IA { //... public: virtual IB* GetB() = 0; }
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.