Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

php

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).

Thanks,
Dan

EDIT: This is the only thing I have found on the web:
http://cstruter.com/blog/44 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.

like image 524
Dan Avatar asked Dec 16 '10 23:12

Dan


3 Answers

Find another language.

Weak typing and JiT compilation is part of what makes PHP PHP. If you feel like PHP's characteristics go against your coding philosophy, then by all means, do not use it.

There are other languages such as Ruby which is strong-typed yet JiT compiled like PHP.

like image 181
Andrew Moore Avatar answered Oct 07 '22 14:10

Andrew Moore


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.

like image 37
Michał Niedźwiedzki Avatar answered Oct 07 '22 14:10

Michał Niedźwiedzki


Have you looked into Eclipse at all? It's a very strong Java and PHP IDE. As well it also allows you to define types within comments. Sure it's not solid... but the code hints do help.

 /**
 * Create a Person
 *
 * @param   string  $name
 * @param   int $age
 * @return  String
 */
function person($name, $age) {
    $this->name = $name;
    $this->age= $age;
    return "Hello ".$name;
}
like image 36
cdnicoll Avatar answered Oct 07 '22 13:10

cdnicoll