On 25 February 2016 at 22:43, Adam Harvey <ahar...@php.net> wrote:

> Why is having a special syntax that is considered callable and
> conflates instantiation and method calls  better than having the injector 
> accept a class name
> and use reflection as required to instantiate that?
> They're
> trivially special cased without muddying instantiation and method
> calls. Hence my "are there analogues for this elsewhere" question.

I think this is actually something that would be useful across
applications - not just my own library.

Although I could use the Reflection code that Johannes wrote to make
it possible for my own code to use constructors as a callable, I
wouldn't be able to use it in anyone else's code, unless I asked them
nicely to add the special casing to their library code as well.

For example I would hope to use callable constructors with the Zend
service manager. That is a good example of where just being able to
specify "Foo::__construct" in a config file would save having to write
'boiler plate' instantiation functions for every object.

I think there is a small 'chicken and egg' problem here. Of course no
one is currently using constructors as callable, because it isn't
possible yet, and so it's hard to point to any code that uses them.
And so it appears that it's an small use case. I'm pretty sure if it
was adopted it would become a standard thing to do when 'wiring up'
applications, or hydrating objects fetched from various data sources.


Rowan Wrote:
> I would like to join Adam in asking if
> you can find examples in other languages where constructors can be used
> as though they were static methods in this way.

Python would be a good example. constructors are just functions that
can be called, and passed around as 'callable' things.

Obviously Pythons concepts of classes aren't identical to PHP'
concepts - but the code is doing the same thing; passing around a
thing that can be called without having to use Reflection to make it a
callable.


http://goo.gl/SnTNZH

==== Begin
from random import randint

class Foo:
    def f(self):
        return 'I am foo'

class Bar:
    def f(self):
        return 'I am bar'

if randint(0,1) == 0:
  classname = Foo
else:
  classname = Bar

obj = classname()

print obj.f()

==== End

I'm not aware of any languages that contemplated making it be
callable, but then decided it would be a bad idea.

cheers
Dan

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

Reply via email to