3

I often use type-hinting when creating things like observers eg.

public function obsSalableAfter(Varien_Event_Observer $observer)

I've found this to be a useful notation to developers if docblocks are missing, as well as obviously the underlying benefits to PHP. However, I came up against a problem yesterday, where I installed an extension into an existing store, and it had a type-hinted a specific function. It turns out, something in the shop I'd inherited was already over-riding this, and PHP was throwing an error due to the mis-matched type.

So, that got me thinking - is type-hinting of any kind good in extensions that are being released to work across multiple stores, since you don't know anything about the target enviornment?

1
  • Because of the type hinting, at least you know where the error comes from. If you didn't use type hinting, the code would just have continued processing with the wrong object type. That would have most likely caused other errors later on in the code. Commented Oct 22, 2015 at 23:28

1 Answer 1

3

Well this only happens if you have a rewrite conflict I think. Then it is even better if you clearly SEE that there is an issue. Otherwise you may not even notice the conflict and will think everything is okay, but problems will still occur.

And I think one can expect that Magento shops have a proper dev system and check new extensions there, so that the issue will not directly effect any live systems. And if it does, it is their fault anyway :)

Hence, I think it is definitely best practice to use type hints where possible. It clearly states what is expected, helps your IDE to show proper hints and helps you as a developer to directly see what you are dealing with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.