views:

111

answers:

7

hi,

i am writing a web site with a php on the server side. which i have never used before. if i get it right it is procedural programming (which is another thing that i have never done)

my question is how to write code: each procedure should be in a different file? to separate it to various files? maybe have one file that receive the request and then dispatches it?

in ojbect oriented things are a lot more clear...

please advise

+4  A: 

You can do object oriented programming in PHP, and I prefer to structure it that way.

If you choose to stick with procedural style, 1 file per procedure is not the way to go, imagine if in OOP you did 1 file per method!

Grouping things is better, maybe you have a DB abstraction layer, in OOP you might make it a class, in 1 file. In procedural implementation, you'd have a bunch of procedures in 1 file. Probably named similarly.

Don Neufeld
thanks ill check out the object oriented option, all this procedural programming feels totally weird and throws me back to the pascal i learned in junior high... brrrrr
special0ne
If you have trouble finding it in the docs, look here: http://www.php.net/manual/en/language.oop5.php
Don Neufeld
+1  A: 

Group related procedures together. One file per procedure is overkill.

There's good advice on this sort of thing in Code Complete. It is a very reputable programming book with good guidelines on how to organize your code.

Parappa
+3  A: 

There is no strict definition of where things need to be placed in PHP. It's entierly up to you. I suggest you check out the MVC pattern if you are looking to write complex systems.

Feel free to use an object oriented model in PHP, its my style of choice now.

Steve H
A: 

PHP can be object oriented.

But if you are not using it that way, then you just need logical divisions for your functions.

If you have just a few functions, use one include file with all the functions in it.

If your site has several areas, such as 'Membership', 'Library', and 'Resources' and each area uses a number of functions (like 5+), consider creating files for each area, such as: membershipFunctions.php, libraryFunctions.php, and resourcesFunctions.php.

menkes
+1  A: 

Personally,i've never jumped in the OO wagon. Old habits die hard. But i experimented with cakePHP which introduced me to the MVC pattern (Model View Controller). Since then i usually follow this structure:

.htaccess << redirects all request URIs to index.php
index.php << the main processing file
_lib/  for configuration files
_lib/classes for all 3rd party classes (such as database abstraction, etc)
_views/
_controllers/
pixeline
A: 

We tend to adopt a structure where we define our objects specific to the site in 1 file. Another file contains all the generic functions (our general library). There's another file defining all the constants and basic structures.

Implementation of the objects and the core site code (database, authentication, etc) is in a common file. This file includes the objects, constants and library files and performs the foundation operations of the site.

Finally, the site's functional groups are placed in their own files for page generation. These files include the common module (file).

Objects are coded to manage themselves so there is little work going on in the functional page files.

We used to split out all the objects into their own files but found it easier to assemble them into 1 general object class definitions file.

Gerard
A: 

I prefer the OO style for PHP 5, and specifically, enjoy the structure that Zend Framework promotes, using PEAR style directory trees, and allowing you to segment your code in a fashion that makes sense, from a OO point of view, and an architecture point of view.

I would delve into the realm of OOAD, but I am pretty sure there are alot of sources on it, one of the best being the Code complete book mentioned earlier in this questions responses.

Look at http://framework.zend.com for a layout of a zend project, particularly the quickstart, I think you might find that it presents you with a good structure to use, even if you dont want to use the Zend Framework itself.

JC