On 26/02/2016 17:08, Dan Ackroyd wrote:
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

Looking at your example at first, I couldn't see how it demonstrated any such passing, because you can dynamically select class names in PHP (here's that code transposed line-by-line to PHP): https://3v4l.org/AD2m3

Looking closer, that's not actually what's happening, but it's still not equivalent to what you want in PHP:

- The first difference is that "Foo()" rather than "new Foo()" creates an object; the fact that "Foo" is a class rather than a function causes this to happen when it is invoked. This is different from PHP, where classes and functions don't share a namespace, and a class is not something that can be invoked ("foo()" doesn't refer to the same thing as "new foo()"). - The second difference is that you are not passing around a string representing what to call, but a reference to the invokable object itself. The line "classname = Foo" simply makes "classname" an alias for the class "Foo", which is then invokable as before to trigger instantiation.

So if anything, this particular example is more like my Callable::forConstructor() idea - a way of passing around an invokable object which, when invoked, will cause instantiation of a particular class.

I don't, however, know Python well enough to say whether a better example would be more like what you want in PHP.

Regards,

--
Rowan Collins
[IMSoP]


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

Reply via email to