( resending with correct formatting, and missing context while at it, sorry 
about that )

On Aug 15, 2012, at 8:33 PM, Nikita Popov wrote:

> (...)
> 
> Another aspect here is that there is no reasonable syntax for this
> feature, at least I can't think of one:
> 
> * The syntax `$foo = (InterfaceName) $container->service` is
> completely out of question. It looks like a cast, but wouldn't
> actually do a cast.
> * Same is to be said about `InterfaceName $foo =
> $container->service`. This syntax implies that the $foo variable will
> always be of type InterfaceName, even if it is later reassigned. It's
> not a sensible syntax for a one time validation
> * The other three syntaxes that were mentioned were just as unclear.
> E.g. `$foo = $container->service as InterfaceName` again looks like a
> strange cast syntax and `$foo = $container->service is InterfaceName`
> looks like the assignment should evaluate to a boolean (i.e. `is` is
> some kind of `instanceof`).


good points
In addition, the root cause that triggered this proposal in the first place: 
serve container / object registry doc issue, is kind of moot, ref bellow.


> 
> On the other hand, the current ways of accomplishing the same goal are
> well-established and easy to understand:
> 
> * Using a docblock: /** @var $foo IntefaceName **/
> * Using an assertion: assert($foo instanceof InterfaceName).
> 
> I think that the assertion is a rather concise and clear way to do
> this. It is much more obvious than some new and obscure `$foo =
> (InterfaceName $container->service)` syntax.



To expand on that, @Stan: If you make sure you configure your container to 
injection dependencies (and in case of lazy loading, inject factories* with 
type hinting in doc) instead of passing the [dependency injection] container 
around as a registry, then you almost don't get this problem at all, and you 
will avoid introducing container awareness/dependency in your architecture.

Only place you will have this original problem though is in the code that gets 
the first dependency of the execution, like in index.php for instance, but even 
then you can have Container interface for the most common root dependencies.
In the cases that doesn't fit, you should plainly use the doc block referred to 
by Nikita, it is already supported by IDE's and won't need additional 3 years 
to get support.


* As alternatives to factories with or without container knowledge, there is 
one alternative that some containers support:
  Proxy objects: container generates proxy objects whenever you want 
dependencies to be lazy loaded, they have container awareness, but this becomes 
100% transparent to your code, but it probably adds some overhead.

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to