# Any chance of having __invokeStatic()

- Thread: https://externals.io/message/56657
- Original archive: https://news-web.php.net/php.internals/56657
- Started by Dmitri Snytkine on 2011-11-28T20:02:14+00:00
- Messages: 3
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #56657 — Dmitri Snytkine — 2011-11-28T20:02:14+00:00

URL: https://externals.io/message/56657#56657

I think __invokeStatic() would be a very useful magic function, especially
with support for late static binding.
you could difine it one and all sub-classes will have that option of
basically being callable classes.

MyClass($params) could then do some processing and return some data.

This would basically be the same as doing MyClass::doSomething($params);
only without the extra typing of ::doSomething



Dmitri Snytkine
Web Developer
Ultra Logistics, Inc.
Phone: (888) 220-4640 x 2097
Fax: (888) 795-6642
E-Mail: dsnytkine@ultralogistics.com
Web: www.ultralogistics.com

"A Top 100 Logistics I.T. Provider in 2011"

---

## #56658 — Nikita Popov — 2011-11-28T20:11:28+00:00

URL: https://externals.io/message/56657#56658 · In reply to: #56657

How would this be different from defining a function MyClass?

Nikita

On Mon, Nov 28, 2011 at 9:02 PM, Dmitri Snytkine
<dsnytkine@ultralogistics.com> wrote:
> I think __invokeStatic() would be a very useful magic function, especially
> with support for late static binding.
> you could difine it one and all sub-classes will have that option of
> basically being callable classes.
>
> MyClass($params) could then do some processing and return some data.
>
> This would basically be the same as doing MyClass::doSomething($params);
> only without the extra typing of ::doSomething

---

## #56659 — Evan Coury — 2011-11-28T20:45:05+00:00

URL: https://externals.io/message/56657#56659 · In reply to: #56658

> How would this be different from defining a function MyClass?

Actually, I just found myself wanting this the other day. The class
name in question was being defined in a config file, which the user
would be free to override with their own class name (and a closure was
ruled out because this config file could be xml/ini/etc). There are
plenty of other ways I decided I could work around this, but I did
stop and think that something like __invokeStatic() would be nice.

My main rationale for wanting this was that classes can be resolved
via an autoloader, whereas functions cannot.

However, I could definitely see where this could get sticky. For example:

<?php

function MyClass() {
    return true;
}

abstract class MyClass {
    public static function __invokeStatic()
    {
        return false;
    }
}

$className = 'MyClass';

var_dump($className());

?>

What would the output be in a case like this? Right now, while it's
horrible practice to share names across functions and classes, they
don't actually share the same namespace. Something like
__invokeStatic() would make it ambiguous in some cases (like above),
so I'm not really sure how that could be resolved.

--
Evan Coury
