too many php files

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
michlcamp
Forum Commoner
Posts: 78
Joined: Mon Jul 18, 2005 11:06 pm

too many php files

Post by michlcamp »

I've been learning PHP as I go with a lot of trial and error and plenty or reading, too - and it's been working great, but I think I've missed something along the way.

It feels like I'm creating too many php files to make my site work...each one having a specific script or a single function on it, and I'm wondering if there's a way to put a lot of PHP functions/classes in one file and then call the specific scripts as needed from the php commands on other pages?

I use 'require' and 'include' here and there for saved snippets of code, but can I call specific snippets from a page with a lot of them on it? (sort of like the 'url_link#name.html' in HTML) or maybe like a "php stylesheet"...

Not sure I've been clear enough, but maybe some of you "PHP elders" know what I'm asking...

thanks in advance.
mc
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

There's nothing wrong with includes, unless (in my opinion) you have an index.php?page=this where 'page' is an included page... I feel that kind of coding is unnecessary.

As far as your question, you cannot do that, that I'm aware of.

You may want to rethink your reasoning behind your functions. If they are simple, why not just hardcode them into the script? If they are more complex, or repetitive and you use them a lot, then go ahead and throw them in an include file.

However, I do not suggest having a separate include file for each function. Have a page called 'functions' or something of the sort where you have all of your most used functions. There's definately nothing wrong with doing it either way. If the code is working, then it works, and the only thing more you could ask for is readability and good logic.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

It could be done, but would probably involve opening the file, regex parsing it then eval'ing the particular section you wanted. By the time you've done all that you could have probably included the whole file in the standard way with far less overhead.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

include the file, if you coded it right then it will not cost you any amount of time that you would ever notice. it is easier to manage 1 header page with like 6 inclues for whatnot then on ever subpage just include the header file which in turn includes the other pages and makes coding more easy to manage (at least for me)
Post Reply