views:

169

answers:

8

Hi,

How do you manage your php codes? Do you prefer functioning within one php file or including larger blocks of "raw code"?

Edit: In fact, my code is pretty nasty, as I don't use any namespaces and classes - only functions and including. I shall look the classes up ^^.

+1  A: 

When I used to program in PHP I liked to group general utility functions in a common file to include in most of the pages, and group classes in dedicated files, to load them only when needed.

tunnuz
Couldn't you put your utility functions into a utility class?
MrChrister
You can (and probably should), but then it's not really a class, it's a faux namespace.
le dorfier
+3  A: 

If you are using php classes, this will sort itself out. If you are not, then it's really hard to give an acceptable answer, except that you should learn to. All php code I've seen done either way without classes seems to become quickly messy.

le dorfier
+3  A: 

I agree that OOP is the way to go. Large solid blocks of code are nightmare for maintenance. Definately not the way to go. You should split your code into small blocks that interact with each other and are easily maintanable on their own.

Vilx-
+1  A: 

Use them as you need them. I use include for chunks of big code doing processing, and functions for "utility" functions. Sometines i use includes within function also... it really depends on how clean you like your code.

Think that many includes means more fopen() from the PHP module, and those can slow doewn the whole script execution..so dont try and put too many includes though.

Quamis
A: 

I typically use functions/classes for logic and includes for display. I end up with something like this in a controller...

case 'widgetlist':
  $widgets = $DAO->getWidgets();   //get some query
  include('view/showWidgets.php'); //assume a global $widgets variable
  break;

I have found it easier to give an HTML/CSS designer an include rather than a function call which displays. The down side is that I rely on globals to pass variables to the include rather than arguments which are much safer.

Nick
A: 

I make classes in separate files, with the correct prefixes as namespace (until they are included at least). I also put functions as static methods in "static classes" for the namespace effect.

I use autoload to include the files so I don't have to write a hundred includes. Save My_Example_Class as {lib}/My/Example/Class.php

OIS
A: 

The thing I'm working on has one included file at the top of every page that contains all the global functions and database setup stuff. It works as-is but I'm now moving the functions into separate files, because with everything in a big lump it's completely impractical to do any testing.

Ant P.
A: 

PHP has an __autoLoad () magic function that you can use to intercept class calls. If the class doesn't exist yet, you can setup some simple code to go and look for the proper class code to include. It will then continue to execute as normal.

Nolte Burke