Modular Development, Reusable code & Project Organization in PHP

Discussion in 'PHP' started by r_honey, Apr 4, 2009.

  1. #1
    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??

    How does CMSes like Drupal, Joomla tackle this problem as they also should need to include many files in each script??
     
    r_honey, Apr 4, 2009 IP
  2. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I use a .htaccess rule that sets auto_prepend_file to ensure that my object autoloader is always available.

    As for common class libraries, I keep them in a set of master directories and then symlink into project directories as required.

    Since I always publish with rsync, it's an easy matter to add --copy-unsafe-links and then I always get what I need on the live server.
     
    SmallPotatoes, Apr 5, 2009 IP