Classes: how best to connect them to other code?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
toro
Forum Newbie
Posts: 10
Joined: Wed Jul 03, 2002 8:03 am
Location: Iceland

Classes: how best to connect them to other code?

Post by toro »

Hello everyone,
I'm taking my first steps in PHP programming and now it's time for my first 'real project'. Rather than code everything in the (few) pages I have to create - I'm thinking of using a Object Oriented approach.

My question is where should I keep my classes? I saw somewhere an example where the classes were all put in a single fleh.inc file and included at the start of each page.

I guess I might create something like 10 classes and am wondering if I should have them all in one file and include it in all my pages - or if I should group them together in 5 files (or so) and include only those containing classes that I'll need for each page.

NB. I haven't started any real design yet, the project is still only in my head but I'd appreciate tips on how you'd do this.

The balance in my mind being between simple coding and minimal load on the server. Does it affect the server noticably to include uneccesary lines of code?

Any suggestions?

Thanx, Toro
User avatar
martin
Forum Commoner
Posts: 33
Joined: Fri Jun 28, 2002 12:59 pm
Location: Cambridgeshire

Post by martin »

Whatever you decide for your classes, please don't use a .inc file. If someone is lucky they may stumble across the file and their browser will ask if they wish to download the file, should it contain database login details or other vital info you could be in do-do. Give the file a .php extension and the browser will just show a blank page.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Yeah definitely don't use .inc extensions for any of your PHP pages (unless the server has been set up to parse .inc as PHP). You can always use filenames like myclass.class.php, myclass2.class.php instead.

Also try to keep each class in a separate file and only include those you need using include_once().

Check out this thread for a discussion on OOP and PHP:
http://www.devnetwork.net/forums/viewtopic.php?t=1089

Enjoy your programming.

Mac
User avatar
toro
Forum Newbie
Posts: 10
Joined: Wed Jul 03, 2002 8:03 am
Location: Iceland

Post by toro »

Thanks guys,
this answers my questions. Now I just have to find me some spare time to start coding and then I'll probably have more questions.

Cheers,
Toro
Post Reply