473,771 Members | 2,328 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1488
On May 29, 3:54 pm, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.comwrot e:
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_ex ists('__autoloa d')) {
function __autoload($cla ss_name) {
require_once($c lass_name . '.php');
}
}

ini_set('unseri alize_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********@gma il.comwrote in message
news:11******** *************@k 79g2000hse.goog legroups.com...
On May 29, 3:54 pm, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.comwrot e:
>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_ex ists('__autoloa d')) {
function __autoload($cla ss_name) {
require_once($c lass_name . '.php');
}
}

ini_set('unseri alize_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.comwrot e:
"ZeldorBlat " <zeldorb...@gma il.comwrote in message

news:11******** *************@k 79g2000hse.goog legroups.com...
On May 29, 3:54 pm, "Jon Slaughter" <Jon_Slaugh...@ Hotmail.comwrot e:
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_ex ists('__autoloa d')) {
function __autoload($cla ss_name) {
require_once($c lass_name . '.php');
}
}
ini_set('unseri alize_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
9600
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, while an instance of class C should be able to check all methods #defined in C, B and A. #------------------------------------------------
1
3343
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 to turn a previously made String class that deals with char's into a templated String class that uses the template parameter C instead of char. I thought it would be fairly simple to do this exercise, but I encoutered many errors for my...
1
1439
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. My case is getting the error, not because methods need shared, but because forms need shared. Example: A.DLL has form TestA1, TestA2 B.DLL has form TestB TestA1 navigates to display TestB, thus A.DLL needs to reference B.DLL so the form will...
15
2783
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 as member variables. So including a forward declaration doesnt help, does it? Faced with these, I had the following options: -Include the appropriate header in the header file that contains the class definition that has a member variable that is...
9
4996
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 pointer which points to an instance of a derived class, but when I pass that base class pointer into a function, it can't access the derived object's public functions. Although, the base class pointer does call the appropriate virtual function...
5
3164
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 CommonPageBase - Contains myPage which inherits PageBase Each of these classes overrides OnInit and ties an event handler
23
3860
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? Here's what I thought should work, but apparently doesn't: class Foo; void f1(Foo* p)
22
4810
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 now at this code: Base *b = new Level2(); Level1 *l1 = dynamic_cast<Level1*>(b); Should dynamic_cast return a valid pointer or 0? I wonder as Visual Studio 2005 returns a valid pointer while g++ 3.4.6 returns 0. Both compilers work as expected...
6
2417
by: kepeng | last post by:
There are 2 abstract base classes: class IB; class IA { //... public: virtual IB* GetB() = 0; }
0
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10260
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10102
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9910
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.