Forum Moderators: coopster
You want to write your code so it's 'modular' - i.e., avoid having one huge file that does everything based on what parameters it gets. Rather, for different page functions, dynamically include other files with functions or classes you need. Be sure, though, to follow proper security proceedures when doing so - use a strict naming convention so you can check the parameters coming in to see that they match the name of a file that's a candidate for dynamic inclusion. E.g., name the files you want dynamically included 'thisfunc-di.php' with the incoming parameter thisfunc -
if(preg_match('#^[a-z]*$#', $_GET['func'])) include $_GET['func'].'-di.php';
You want your code to be expandable, updateable. Make the general architecture so it's easy to add functionality - either new types of pages, or new stuff on existing pages. So when you write pages, in addition to the standard header/footer stuff, program and design so there's a space where stuff can easily be added. Arrays and dynamic includes are your friend.
Ideally, then, you should just be able to add a certain submodule functionality to a whole set of a certain type of pages (or module) by adding a couple of lines somewhere - you can have an array, for example, that's a list of the submodulesmodules to be loaded, have the files necessary for these submodules included and the functions called, and a space to display the output. You have to be sure, though, that you load things up, set variables, and call functions in the right order. Suppose, for example, you have a main page module that has an "author id" in its db table. Later you make a module displaying information on the authors - mugshots, favorite tea, etc. Calling the author submodule from your articles module, you need to be sure that the author id of the main articles module is already loaded in before the authors submodule function is executed - which, e.g., gets the url of the mugshot and url of corresponding author page, and makes a variable available for displaying it. This variable is then output in the special box you reserved for extra content.
Keeping all these modules and submodules in their own directories - e.g.
modules/articles/main.php
modules/author/submodule.php
will help you keep your ducks in a row when, in six month's time, your client also want's the author's favorite brand of tea displayed.
Well-written content management systems will give you an inspiration base for organizing your code and making it more extendable.
Object-oriented programming will also help. However, I only use it for admin-type functions, and if you can't keep your ducks in a row with file-module style programming, your object-oriented programming might help streamline things a bit, but if you don't learn discipline in the act of coding this way, your code will be just as alphabet-soup like as any proceedural coders'.
This is a very interesting question and I wish that there were more on the web written about it. Thanks for asking, ocelot.
well I looooooooooove OOP, having been introduced to it by C#. it's so wonderfully descriptive and clean and non-confusing.
but how does that work in php? I know you can make your own classes and objects in php, but they're only visibile within the file they're created in right? so then it's still a big pain if you have a large script that you want to break up among files.
I wish there were something like namespaces in C# where it doesn't matter what file a class is in or what directory. as long as you tell the compiler where all your source files are, it takes care of the rest - allowing you to keep things nice and organized.
but how does that work in php? I know you can make your own classes and objects in php, but they're only visibile within the file they're created in right?
[...]
I wish there were something like namespaces in C# where it doesn't matter what file a class is in or what directory.
Actually visibility for classes and functions in PHP is global. So long as you include() the file that defines the class before use, you can instantiate that class without problems.
PHP is much looser with class declarations than most languages. There are no compilation units, so you're not restricted to one class per file or forced to name your file the same name as your class.
Of course, that flexibility also gives you room to arrange things totally insanely. :)
I noticed recently that PHP 5 has an flexible autoloading mechanism for classes. You can define a function that, given a classname, actually finds the source file and loads it. PHP calls your function automatically when it needs a class. Pretty handy.
One might wonder why not just do an include('*.php') sort of thing, and load everything? As an interpreted language, each request needs to actually parse and compile the source. If you load code you don't use, you're burning time you don't need to. With some good organization and judicious use of require_once(), you can load your codebase on-demand and keep your sanity.