dan dan - 5 months ago 6
Java Question

How to make PHP more Javatized (aka strongly typed, more robust)?

I really love PHP because:

_ easy to develop web applications (you set up LAMP in 10 minutes and you are ready to go)

_ easy to learn

_ easy to deploy (you just need Apache with the PHP module)

I really love Java because:

_ it is strongly typed

I was amazed by developing with Java and Netbeans. Any time there was a type mismatch I was alerted by the IDE while developing and the IDE itself was suggesting the fix.

I think strong typing is a bliss. That's true: PHP arrays that can contain anything speed up your development, but then you have to spend time finding bugs while with a little more work ahead you have the IDE that points out the problems for you.
That makes me feel more confidence also during refactoring.

So, my question is: is it possible to use types in PHP (including for the return value of a method)?
Do you know of a project, a fork, anything that would allow me to do that?

It could be just a pre-parser that checks some comments before the actual PHP parser (i.e.

public function /*ConfBuilders[]*/ getConfBuilders(/*int*/ confId)

Please anything that would introduce a concept of type and, thus, in my opinion, would make a PHP application more robust and, eventually, quicker to implement (right now I think it is quicker to develop software in Java than in PHP, thanks to the IDE's help).



This is the only thing I have found on the web:

but I don't like it, mainly because you can't declare the object properties in a standard way and for sure make the execution of the script slower. I would like a method that works offline, while developing...a sort of compilation.


There is a couple of ways of making your code more rigid.

  1. Use type hinting in methods to restrict argument type. This works for classes, interfaces, and arrays only. See example:

    class Foo { }
    class Bar {
        function quux(Foo $f) { }

I use type hinting as much as I can to save myself from silly mistakes like passing null or primitive value whereas object is desired. Return values are unfortunately not strongly typed.

  1. Use access qualifiers: public, protected, private. While public members can be accessed by an outside entity, protected and private can be only accessed by instance of the same (base) class. Also consider using abstract and final qualifiers.

  2. Code against interfaces rather than classes, use composition and aggregation aggressively. Get used to use of dependency injection. Use well known design patterns where appropriate.

  3. Familiarise yourself with elements of Reflection and Aspect Oriented Programming. While there's very little PHP has to offer in AoP area, its reflection subsystem is quite good. You can use @annotations in doc-block comments to take advantage of metaprogramming. I use it a lot to annotate classes with location of unit tests. I also have an implementation of ACLs where controller methods are annotated with (to make long story short) requisite access level:

     * @AclActivity('view-services')
     * @AclActivity('edit-services')
    public function editServiceAction() { ... }
  4. Finally, don't restrict yourself to PHP. Read about other languages, not only mainstream ones, but also obscure ones - these are full of brilliant ideas. Check out Lisp, Erlang, Rebol. Find unusual applications for existing languages, like server side JavaScript programming. Write your own language. Yes, it's going to suck, but you will learn a lot.