test.ical.ly | getting the web by the balls



Automatic class loading and naming conventions with Symfony2, PHPUnit and PHP 5.3

I mentioned yesterday that I am toying around with some little library code that is completely standalone.

As I plan on using this code in a Symfony2 project later I chose to finally switch to PHP 5.3 including namespaces.

And as soon as I started I needed to decide on a naming convention for my classes, a directory structure, a convention to name my PHPUnit tests and a way to autoload these classes to avoid require statements.

Of course I don’t fancy to come up with all that on my own. In fact I’d rather prefer to use existing conventions and luckily I found one that is used in Symfony2 but was in fact created by a group of PHP developers worldwide from all kinds of projects including the Zend Framework. This group of people is called the PHP Standards Working Group and they agreed on a paper called PSR-0 Final Proposal.

Everything defined in there pretty much reminds me of the Zend Framework conventions only much much simpler as namespaces are a very efficient way to organise code.

So I went and created a simple dummy library with the following:

  • Two stupid classes with one method each
  • Two PHPUnit tests
  • All namespaces set up just like in Symfony2 for library code as well as unit tests
  • A demonstration of a namespace type hint
  • A PHPUnit configuration file for distribution
  • A bootstrap file initialising the Symfony2 universal class loader

I think for those of you interested it will be far more convenient to take a look on GitHub or clone the sources instead of me writing down snippets of code. So here you can find my example code on GitHub.

You can see it’s dead simple. Even the autoloading is nicely tucked up.

Tell me what you think!

· · · ·

  • Pingback: Automatic class loading and naming conventions with Symfony2, PHPUnit and PHP 5.3

  • Pingback: Tweets that mention • Automatic class loading and naming conventions with Symfony2, PHPUnit and PHP 5.3 | test.ical.ly -- Topsy.com

  • http://www.robo47.net robo47

    Wouldn’t namespaces based on the the directory-hierarchy for the tests make more sense ?
    Don’t know if there are any use-cases where autoloading (instead of phpunits lookup) would be needed for the tests, but compared to what is used for lib itselfs, this would make more sense to me:

    File: tests/Example/baseTest.php
    Namespace: \tests\Example
    Class with namespace: \tests\Example\baseTest

    File: tests/Example/Sub/BarfooTest.php
    Namespace: \tests\Example\Sub
    Class with namespace: \tests\Example\Sub\baseTest

    Would allow for other tools (eg one that analyses unittests or something else) to autoload the tests too and test-namespace/class can directly be created by just prefixing the class-namespace with tests and appending Test (Thinking about Netbeans which allows to jump to tests with a shortcut from a class and vice-versa).

  • http://test.ical.ly Christian

    @robo47 you spotted the one thing that I had problems with as well. It would seem coherent to have \Test\Example namespaces instead of \Example\Test looking at the directory structure. But it also makes sense to make the tests namespace part of \Example instead of the other way around. I was struggeling with that and since I couldn’t decide I just followed the practice in Symfony2.

  • http://www.delegate.at Manuel Grundner

    @Christian When you use autogenerated Teststubs from PHPUnit the Testclasses are always in the same namespace of the to testing class.

    @robo47 The JumpTo-Function of NetBeans works just fine with the method above described

  • http://test.ical.ly Christian

    @Manuel thanks that’s good to know!



Theme Design by devolux.nh2.me