PHP5 - Modular programming, and project organization

In .NET & Java, modular programming is implemented as reusable assemblies or jar files, which can be referenced from other projects.

However, I found no such approach available in PHP. There is no way to write code that can be used across projects in a central location, and reference it from multiple projects. I need to copy/paste it manually to project directories, and keep them updated.

A bigger problem is include files. You have to include the file containing classes so that the classes are available. Now, I have begun working on a PHP project, that might end in excess of hundreds of classes. My way of programming is to keep each class in a separate file and logically organize classes in folders. Now how should I ensure that each script includes all files that contain classes it uses??

I though of one option. To organize the classes in namespaces, and arrange them in folders named after thir namespaces. Then I would implement the __autoload magic function, and include the file:
[Root]/MyCompany/Util/XmlUtil.php

corresponding to the class name:
MyCompany/Util/XmlUtil

when that name is passed to the _autoload function. But soon I found out, that namespaces were made available in PHP 5.3, and the server I am working on has PHP 5.2.8

I am now thinking of to include just one file "includeAll.php" in every script, that would then include all my classes in every script.
Is that a good option in terms of performance or usability?
Does it affect performance to include files in a script that might not actually be used by the script??

Comments

  • i think you are trying to make your own MVC structure, but many are already available, like cakephp and zend framework. you should take a look at them.
  • am trying out cakePHP... and it really makes doing OOP in php a breeze.. i come from a java background and had to learn php out of frustration.. i kept having lots of website design jobs.. now though u can use java to build them... it felt like java would be an over kill for a simple site.. so my journey into php began... after a few months on php.. i decided to organize my code as best as i could to look like my java equivalent..but man that was hard.. until i stumbled upon cakePHP and now i can do my OOP php with ease..
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

In this Discussion