mercredi 18 mars 2015

Type hinting v duck typing

Using the following simple Example (coded in php):



public function doSomething(Registry $registry)
{
$object = $registry->getData('object_key');
if ($object) {
//use the object to do something
}
}

public function doSomething($registry)
{
$object = $registry->getData('object_key');
if ($object) {
//use the object to do something
}
}


What are the benefits of either approach?


Both will ultimately fail just at different points:


The first example will fail if an object not of type Registry is passed, and the second will fail if the object passed does not implement a getData method.


How do you choose when to use either approach?


Aucun commentaire:

Enregistrer un commentaire