Re: __invokeStatic() method
| From: | David Muir | Date: | Mon, 18 Mar 2013 07:49:26 +0000 |
| Subject: | Re: __invokeStatic() method | ||
| References: | 1 2 3 4 5 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-66679@lists.php.net to get a copy of this message | ||
On 18/03/2013, at 6:07 PM, Matīss Roberts Treinis <mrtreinis@gmail.com> wrote:
> Not only that. This potentially might break compatibility with many
> software products already out there. Also, this might lead to many
> misunderstandings and, in fact, ambiguous code. Consider the sample.
>
> $name = 'something';
> $something = $name(); // What is this - a function call or object?
>
> Fact is, we can not know that unless a) do a search in code b) var_dump?
>
>
> 2013/3/18 Pierre du Plessis <pierre@pcservice.co.za>
>
>> On Mar 18, 2013 2:41 AM, "Thomas Bley" <thbley+php@gmail.com> wrote:
>>>
>>> On Sat, Mar 16, 2013 at 9:33 PM, Pierre du Plessis
>>> <pierre@pcservice.co.za> wrote:
>>>> On Mar 16, 2013 9:35 PM, "Daniele Orlando" <dnl.rlnd@gmail.com>
>>>> wrote:
>>>>>
>>>>> Hi List,
>>>>>
>>>>> I'm interested in proposing an RFC and I would know your opinion.
>>>>>
>>>>> === Current Situation ===
>>>>> Since PHP 5.3 we can use an object instance, who defines the
>> __invoke()
>>>>> method, as a callable object.
>>>>> Example:
>>>>>
>>>>> // PHP Code.
>>>>> class Runnable
>>>>> {
>>>>> public function __invoke()
>>>>> {
>>>>> echo "Runned";
>>>>> }
>>>>> }
>>>>>
>>>>> $r = new Runnable();
>>>>> $r();
>>>>>
>>>>> // Output
>>>>> Runned
>>>>>
>>>>> === The Idea ===
>>>>> In Python, when you construct an object, you don't need to use the
>> "new"
>>>>> keyword but you just invoke the class name followed by "()", like the
>>>> class
>>>>> is a function.
>>>>> Example:
>>>>>
>>>>> // Python Code.
>>>>> class A:
>>>>> pass
>>>>>
>>>>> A()
>>>>>
>>>>> // Output.
>>>>> <__main__.A instance at %address>
>>>>>
>>>>> Now, would be interesting to extend the PHP __invoke() method adding
>> an
>>>>> __invokeStatic() method, like happens with __call() and __callStatic()
>>>>> methods.
>>>>> In this way could be possible to use a class name to invoke the
>>>>> __invokeStatic() method.
>>>>> Example:
>>>>>
>>>>> // PHP Code.
>>>>> class TrueRunnable
>>>>> {
>>>>> public static function __invokeStatic()
>>>>> {
>>>>> echo "Runned";
>>>>> }
>>>>> }
>>>>>
>>>>> TrueRunnable();
>>>>>
>>>>> // Output.
>>>>> Runned
>>>>>
>>>>> But the possibility are endless:
>>>>>
>>>>> class A
>>>>> {
>>>>> public static function __invokeStatic()
>>>>> {
>>>>> return new A();
>>>>> }
>>>>> public method m() {}
>>>>> }
>>>>>
>>>>> A()->m();
>>>>>
>>>>> // or
>>>>>
>>>>> class A
>>>>> {
>>>>> private $_instance;
>>>>> public static function __invokeStatic()
>>>>> {
>>>>> // Singleton pattern.
>>>>> if (self::$_instance) {
>>>>> return self::$_instance;
>>>>> }
>>>>>
>>>>> return self::$_instance = new A();
>>>>> }
>>>>> public method m() {}
>>>>> }
>>>>>
>>>>> A()->m();
>>>>>
>>>>>
>>>>> === Conclusion ===
>>>>> This feature makes the __invoke() method consistent with the __call()
>> and
>>>>> __callStatic() methods,
>>>>> and opens the door to many cool stuff.
>>>>>
>>>>> Any feedback is appreciated.
>>>>>
>>>>> Daniele Orlando
>>>>
>>>> I don't really see a use case for this, as you can already use the
>> syntax
>>>> A::method();
>>>>
>>>> E.G class A { public static function invoke() { return new A; }
>>>>
>>>> public function m() { echo 'Runned'; }
>>>>
>>>> A::invoke()->m();
>>>>
>>>> Your example above only saves a few characters to type and can lead to
>> a
>>>> lot of problems if you have a function with the same name as the class.
>>>
>>> Using A::invoke(), you need to know the name of "invoke()" and it's
>>> hard to force users always to use the invoke() function.
>>> Using A() would be more clean since all the static init(), factory(),
>>> invoke(), getInstance() are gone.
>>> Having __call(), __callStatic(), __invoke() and invokeStatic() would
>>> make the overloading concept more consistent.
>>>
>>> Regards,
>>> Thomas
>>>
>>> --
>>> PHP Internals - PHP Runtime Development Mailing List
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>> Using A() looks too much like a function call. And there is still the issue
>> with having a function name the same as the class name.
>>
This works well in languages like JavaScript where functions are objects.
As much as I like the feature there, I'm not sure it's a great fit for PHP.
David
Sent from my iPhone