PHPTypeSafe: September 2008 Archives

Today I released the first public version of my new library PHPTypeSafe. It provides the ability to use PHPs Type Hinting feature for scalar values.

The PHP way of Type Hinting

PHP 5 introduced Type Hinting. Functions were now able to force parameters to be objects (by specifying the name of the class in the function prototype) or arrays (since PHP 5.1). But Type Hints can only be of the object and array type. Traditional Type Hinting with int and string isn't supported.

There were several request on the PHP internals maillinglist to introduce Type Hinting for scalar values, too. All of them got rejected, but at the moment there's a request for comment going on at the PHP Wiki. Besides Type Hints for scalar values the proposals also includes Type Hinting for return values. While the first proposal can be done in the userland, the later can only be archived by changing PHP itself.

So what does PHPTypeHint offer?

By including PHPTypeSafe in your project you will be able to use Parameter Type Hinting for scalar values, the compound type object and the special type resource. A simple example on how to use this functionality would be:

The output would be:

PHPTypeSafe offers the support for the following types:

  • boolean (including alias bool)
  • float (including aliases double and real)
  • integer (including aliases int and long)
  • object (generic object matcher)
  • resource
  • string

Every type hint matcher is strict (so '1' won't match with the integer type hint), as you shouldn't use type hints if you aren't sure about the given /required type of value. (Those cases could still be checked using is_str(), is_int() and the like.)

Requirements and where to get it

PHPTypeSafe has been written using namespaces and is being packaged as a Phar PHP Archive. That's why it requires you to use PHP 5.3 (or later).

You can download PHPTypeSafe at dev.maxhorvath.com.

Feedback

I would be very happy to receive your feedback on how you like the library and if there are any issues on how to use it.

PHPTypeSafe: September 2008: Monthly Archives

Recent Activity

Monday

  • I tweeted, "Just published a new blog post: How to make UILabel / UITableViewCell to have a dynamic height ... http://tinyurl.com/8s55nb"
Wednesday

  • I tweeted, "Flat is prepared for the party ..."
Tuesday

  • I tweeted, "Good morning Twitterverse!"

More ...

Conferences

Conferences I presented at:

Programming Blogs - BlogCatalog Blog Directory
Creative Commons License
This weblog is licensed under a Creative Commons License. blogoscoop