Professional PHP

PHP Programming, Web Development, PHP Advocacy and PHP Best Practices.
« The Usability of Input Filtering
php testing and coverage »

Documentation versus Productivity?

April 4th, 2005

Require once is part of your documenation points out an interesting case.

Consider that you have two programs, A and B. A and B share some duplicated code. Since duplicate code is bad, we factor out the duplicate code into file C (for common). Both A and B include C. All is well. The code is obvious.

However, there is still one last code duplication between A and B. That is the include statement itself. So instead, we wave our magic patterns wand, utter some mystical phrases (”dependency inversion!”) and introduce a front controller. Now, the common code that was in file C is included by the front controller even before A or B is called.

A and B are even simpler because they don’t need the include statement. Depending on the code, they may also no longer have any explicit dependency at all on the common code. C is a little more complex because it has to dispatch to A and B, but when A and B are really A-Z, the dispatch price fades away.

The cost? The application has a bigger learning curve for those unfamiliar with it.

The benefit? The developers familiar with the application have better productivity.

could php’s session_start() function be so simple without the bazillion session.x ini file settings that were established before your program even ran? PHP provides an environment in which your application can be simpler. Large productive projects also do this at the application level.

Its my opinion that any web application of sufficient size can’t help but sprout a front controller at some point.

Should you put a require_once at the top of every file for each dependency it contains? I think the state of the art is going in a different direction, towards containers, front controllers, and dependency injection.

categories PHP
tags

Related Posts

  • On the Perils of Inline API Documentation
  • Comparing PHP with other languages
  • PHP versus ASP
  • PHP Book sales trends versus Java and Ruby
  • Installing Web Applications
You can leave a response, or trackback from your own site.

4 Responses to “Documentation versus Productivity?”

  1. #1 Lucas Carlson responds...
    April 4th, 2005 at 9:08 pm

    Are you familiar with Ruby on Rails? http://rubyonrails.org/

    It uses dependency injection and front controllers very nicely.

  2. #2 Sencer responds...
    April 4th, 2005 at 11:42 pm

    Wether you are doing includes, auto_prepend, explicit Frontcontroller by rewriting every request to a single script - I see it all only as a matter of syntax. There is no inherent superiority with either appraoch. Depending on how all the other code looks, some people may be more comfortable with one over the other, but that boils down to a matter of personal preference. IMHO.

    >C is a little more complex because it has to dispatch to A and
    >B, but when A and B are really A-Z, the dispatch price fades
    >away.

    I am not sure I understand what you are saying here. Is it easier to add lines to the frontcontroller than to the file that would otherwise have an include-statement?

    >Should you put a require_once at the top of every file for >each dependency it contains?

    No, I think most people would just require one file that takes care of including all the other necesary ones.

    >I think the state of the art is going in a different >direction, towards containers, front controllers, and >dependency injection.

    I agree, but I think it’s because it fits better with the (meta-)language that most people speak, it’s neither new nor different from what people were doing before the knew the words or concepts of container, front controller and dependency injection.

  3. #3 Alan Knowles responds...
    April 5th, 2005 at 10:30 pm

    The article acheived it’s aim, in that it encourages coders to think carefully about when and where to put your require_once’s. In a totally OO patterned framework, you probably have a base action class, for a particular module or site, which can load up common libraries that are required. So it is likely you end up with a single require_once on the action page, the inclusion of the base class. Which in turn pulls in some of the more critical libraries (Database tools / Template Tools etc.). Other components (like task specific libraries) are often best moved right next to the location they are used.. the line before the class is first instantated..

    As my own mini-framework has evolved, the amount of libraries autoloaded at the start has been reducing, rather than increasing, moving alot of the responsibilities to the page action base classes, or directly into the code flow..

  4. #4 EViL3 responds...
    April 6th, 2005 at 6:36 am

    I would suggest this comes down to personal preference, I myself define most of the require_once’s in a single file and then pull this into my MVC’s entry point, this way all classes are instantly available without the worry of maintaining dependancies.

    Depending on the project this could be anything from 10 to 100+ includes, and before any of the hardcore-speed-freaks scream how wasteful this is, the overhead of pulling in so many classes can be drastically reduced by using a Cache system such and EAccelerator or Zend Accelerator.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

code: use [code=php][/code].

Comment Preview

  • Search

  • Subscribe

    Subscribe All Posts
    Subscribe All Comments
    Subscribe All Bookmarks
    Subscribe with Bloglines Subscribe with My Yahoo Add to netvibes Subscribe in NewsGator Online Add to Google
  • Share This

  • Categories (Home)

    • Agile Methods (14)
    • Mac (14)
    • Misc (16)
    • Open Source (14)
    • PHP (95)
    • Software Design (28)
    • Usability (14)
    • WACT (7)
    • Web Design (20)
  • Recent Comments

    • Working with PHP 5 in Mac OS X 10.5 (Leopard)  117
      Daniel Wabyick, Ya, Paul [...]
    • PHP Development From Java Architects Eye  12
      peter, qmenqyqh, big [...]
    • Why is PHP Popular?  34
      portfelmake.ru, finance-x.ru, economnedv.ru [...]
    • The PHP scalability saga continues  35
      mvyqfarib, fuck, sorwostajufe [...]
    • Delphi for PHP  29
      Shawn White, Chris MacPherson, Pieter Viljoen [...]
    • How to Transfer Mac OS X Application Data between Computers  41
      ??????????, Edward Gan, Kamut Torok [...]
    • php | tek Wrapup  7
      Sergey Brin, Learn Programming, PHP Guru [...]
    • A WordPress bug fix  5
      Juggigomugs, LkM, wordpress quickstart guy [...]
    • The value of MVC  11
      wan optimization, Ron, Caren Goodman [...]
    • php | tek 2008  3
      CharnaD, NatureLimit, Scott
    • Why is PHP Code Considered Hard to Maintain?  26
      Tom, , Visitor338 [...]
  • Pages

    • Tags
  • Recent Posts

    • php | tek Wrapup
    • php | tek 2008
    • Sarah Snow Stever
    • Benchmarking PHP’s Magic Methods
    • The Endpoints of the Scale of Stupidity on Video
    • Working with PHP 5 in Mac OS X 10.5 (Leopard)
    • Keywords and Language Simplicity
    • Improved Error Messages in PHP 5
    • Michigan Taxes Graphic Design Services
    • Ruby versus PHP or There and Back Again
  • Archives

    • 2008: May
    • 2007: Jan Feb Mar Apr May Sep Oct Nov
    • 2006: Jan Feb Mar Apr May Jun Jul Oct Nov Dec
    • 2005: Jan Feb Mar Apr May Sep Oct Nov Dec
    • 2004: Apr May Jun Jul Aug Sep Oct Nov
  • Menu

    • Register
    • Log in