Skip to main content

Defining Phing Tasks in PSR-0 style

This blog post might be outdated!
This blog post was published more than one year ago and might be outdated!
· 2 min read
Stephan Hochdörfer
Head of IT Business Operations

Before anybody complains: I know that "as of 2014-10-21 PSR-0 has been marked as deprecated. PSR-4 is now recommended as an alternative." - Anyway I still think this little gem makes sense to be shared because a lot of people are probably not aware of it. I recently found out by accident that it is possible pass a task name in PSR-0 style to the "taskdef" task. In the old days you had to use the Java-like dot-style notation like this and also define the classpath to make sure the class could be loaded correctly:

<taskdef name="mytask"
classname="MyApp.Phing.MyTask" />

This is ok if the task resides in the same project. If the task is located in a dependant package loaded via Composer this can get ugly. You need to explicitly set the classpath which leads to a hardcoded dependency in your poject which in the end can cause a lot of trouble. In addition to that you were not able to define a namespace for the class as Phing whould not be able to "see" the class then in it's own autoloader. Luckily you can make use of the PSR-0 classname style and pass the classname like this:

<taskdef name="mytask"
classname="\MyApp\Phing\MyTask" />