Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Put include(a class) in another class file?

Status
Not open for further replies.

Trusts

Programmer
Feb 23, 2005
268
US
Hi,

I am reworking someone's code. The previous developer makes use of classes. That's fine.

I am incorporating an encryption/decryption class I have. It would be useful if some of the other classes could reference and make use of my enc/dec class -within their own class.

I suppose to do that I would have to use the inlcude or include_once function. I tried putting it in, but errored out. I had placed the include right after the class definition, but before any functions. That seemed the logical place - then all functions could use it. But the error states that I cannot place anything in between the class definition and a function definition (except of course for the vars of the class.)

I hope I made it clear enough what I am trying to do. Any suggestions?

THanks,
KB
 
there are two different levels to consider here.

first off you have to make sure that php knows about the code you want to reference. you do this with include_* or require_* depending on what kind of error handling you are after.

you _can_ put this inside the class definition (within a class method of course), but i typically include all files that i need at the beginning of the script. you need to consider the scope in which the code is included too.

you then need to consider whether your code needs to access the properties and methods of the classes. this can be done in two ways:

a) your code can create new instances of the classes (objects) and work on/with them independently;
(b) your new classes can _inherit_ the properties and methods of the parent classes (or vice versa). for more information on this look up object inheritance in the php manual.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top