Would you like to see more type hinting in PHP ?

In: PHP|Programming language

20 Mar 2009

One of the characteristic of PHP is that it doesn’t do strong typing.

PHP 5.1 introduced Type Hinting. Functions were able to force parameters to be objects (by specifying the name of the class in the function prototype) or arrays.

I recently read one RFC (Request For Comments) – on PHP.net wiki – from Felipe Peruna about Return value and parameter type hint. Basically, the idea is to allow to specify what will be the type of the result of a function or method. Here is an example.


<?php 

class test {
    public function (int) foo(){
        // ...
    }
}

In that previous example, the method foo must return an integer, if that’s not the case, then the following error will be triggered : “Catchable fatal error: The returned value must be of the type integer”.

I personally think this could come handy in some cases.

Be Sociable, Share!

Comment Form

Who am I?

My name is Bashar Al-Fallouji, I work as a Enterprise Solutions Architect at Amazon Web Services.

I am particularly interested in Cloud Computing, Web applications, Open Source Development, Software Engineering, Information Architecture, Unit Testing, XP/Agile development.

On this blog, you will find mostly technical articles and thoughts around PHP, OOP, OOD, Unit Testing, etc. I am also sharing a few open source tools and scripts.

  • Trinzia: Well done, my friend! [...]
  • vivek raj: Hello Bashar, It's really good that you wrote this code. but I'm confused some part. can you suppor [...]
  • irfan: I saw watch your youtube talk on clean and testable code. By the way very good talk. I was wondering [...]
  • Mohamed: Hello bashar, I hope you are doing well. Thank you for your hard work, and thank you for sharing [...]
  • alex davila: Hi Bashar is there any pick up example?? Regards Alex Davila [...]