• [^] # Re: The Hack language : PHP avec un peu de typage statique ?

    Posté par (site web personnel, Mastodon) . En réponse à la dépêche The Hack language : PHP avec un peu de typage statique. Évalué à 1.

    Voici la complexité d'un proxy naïf :

    <?php
    class proxy
    {
     private $targets = array();
     public function __construct(array $targets = array())
     {
     $this->targets = $targets;
     }
     public function __call($method_name, $args)
     {
     foreach($this->targets as $target)
     if(method_exists($target, $method_name))
     return call_user_func_array($target, $method_name);
     throw new exception(sprintf('Method \'%s\' doesn\'t exist', $method_name));
     }
    }
    class first
    {
     public function first()
     {
     return 'first';
     }
    }
    class second
    {
     protected function second()
     {
     return 'second';
     }
    }
    $proxy = new proxy(array(new first, new second, new third));
    assert('first' === $proxy->first());
    try
    {
     $proxy->third();
     assert(false);
    }
    catch(exception $e) { }
    $proxy->second(); // This trigger a fatal error
    assert(false);