Page 1 of 1

too many php files

Posted: Thu Nov 10, 2005 5:10 pm
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

Posted: Thu Nov 10, 2005 6:20 pm
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.

Posted: Thu Nov 10, 2005 6:38 pm
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.

Posted: Thu Nov 10, 2005 8:06 pm
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)