PHP5.x or PHP6 – Argument for Type-Hinting: Better IDEs

I’ve read some posting lately about PHP6, the Meeting Notes by the Developers, etc. that deal with type-hinting, especially scalar type hinting. Here is my reasoning why PHP should allow for more type-hinting scenarios: it allows IDEs to be more powerful. An example would be auto completion. Many times an IDE can’t possibly know what is being returned by a function or what is stored a variable because PHP is not strongly typed.

An example from another language is ASP .NET. It is strongly typed, which means there is almost zero guess work what is stored inside variables or what is returned by functions. The reason why I think .NET is so popular is that Visual Studio is so powerful. It’s features with auto-completion and refactoring are incredible.

However, being strongly typed can have heavy disadvantages for many developers. Some things that are easy to do in PHP that can be very complex in ASP .NET. This is why I prefer PHP over ASP .NET.

Some IDEs like Zend Studio Neon try to accomplish many auto-complete and refactoring abilities by enforcing strict include patterns and Doc Blocks. This can be more restricting and cumbersome than many restrictions enforced by strongly typed languages while still failing to provide really good features that VS.NET has.

So this is why I am pro PHP having a very complete set of type-hinting instead of being strongly typed. This allows for the flexibility that PHP provides, but also allow developers to integrate good-practices by using type-hinting in many scenarios, not just in function parameters.

A real life example is trying to use a class within a View Script (i.e. myview.phtml) that was set to a Zend_View by the Zend_Controller_Actio:

Setting the Variable:

function myAction()
$object = new MyClass();
$this->view->object = $object;

Now, my inside that file while I edit Zend Studio Neon can know $object is an instance of MyClass, so it can auto-complete all of the members and methods of that class.

However, inside my View Script:

$object = $this->object;
echo $object->myFunction();

My IDE has no way of knowing what $object is, because while editing it can’t know this file will be included by a Zend_View class, and because $object set dynamically at run time my IDE can’t know $object is an instance of MyClass. Lets say I’m going to use $object’s members and methods over 50 times in this file. It would save me a lot of time from only having to type so much and having auto complete finish my entries, as well as help prevent typos. Even if a typo slipped in, I’d hope PHP during run time would throw an error, like an E_STRICT, and continue to run. This would allow for best-practice coding that would help prevent errors.

Example of what I’d want:

$object = <MyClass>$this->object;
echo $object->MyFunction();

Now the IDE could know “Oh, this is a MyClass, I’ll make his life easy and enable auto-complete and refactoring”.

Some may say this is frivolous and I’m not preaching the “PHP Way”. After all, the developer’s decided that type hinting isn’t the “developer’s” way. I’m already forced to use DocBlocks to declare what members store and what methods return so Zend Studio can predict what to auto-complete. Adding more DocBlocks to support type hinting anywhere does not help.

The ultimate goal in my mind is to help PHP be more corporate friendly. My current company doesn’t want to invest in developing PHP applications because there are no good tools to help code in PHP. Its just scripts. Of course this isn’t the only reason, but in my experience the more large and advanced a project becomes, the less PHP is apt to help solve problems large projects face. If I have a large project with 10 programmers, I’ve found myself reading other’s code to make sure I know what is what instead of having my IDE help me out.

Hopefully Type-Hinting will eventually be embraced by PHP.

6 thoughts on “PHP5.x or PHP6 – Argument for Type-Hinting: Better IDEs

  1. I thought php had
    $object = (MyClass) $this->object;
    ? no?
    Maybe it was just (array) & (object)


  2. Couldn’t agree more with the article. I read the notes from the developers meeting and was disappointed. Am relying on what feels like hacks from IDE applications to keep my faith in PHP.


  3. The IDE would know the object type from the docblock of the member variable.


  4. I know, but there are instances where my PHP code is written in a way that the IDE can’t detect the type. I’d also like for my PHP code to throw warnings or errors if the Type was broken.


  5. PDT 1.0 supports this kind of annotation:
    /* @var $object MyClass */
    PDT 2.0 seems to have regression, unfortunately :/


  6. Heh, after going back and reading this post, looks like type hinting will be a very, very long way off.

    The good thing is (like Adam stated above) my current IDE (NetBeans 6.9.1) has support for the variable declaration.

    /* @var $variable MyClass */ works great. It also, which helping the auto completion in NetBeans, also helps other developers know what value a variable has, even outside of the IDE.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this:
search previous next tag category expand menu location phone mail time cart zoom edit close