On Mon, Jan 23, 2023, at 9:39 AM, Ollie Read wrote:
> You are absolutely correct. I guess the solution would be to handle it
> differently in this case.
>
> Creating a closure from a static method would be fine, as it creates a
> static closure, but when attempting to create a static closure from a
> non-static method, it would instead return a closure that errors if it
> isn't bound to an appropriate object. You'd most likely want to
> restrict this to public methods only, which would help with the
> security issues.
Then don't make it a non-static method? Using an object method as a static
method has been bad practice for as long as I can remember and an error since
sometime recently, I think, so let's not do that.
> There's already a check there that throws an error, so we can already
> tell the difference there, but the tricky part will be in the returned
> closure. Perhaps something like "BindingClosure" that throws the static
> error when attempting to call it unbound, or better yet, a more
> descriptive error about it requiring binding.
>
> Would that be feasible?
By the time the error is thrown is too late. It's a distinction that needs to
be made before the bind. So you could do like:
if ($fn->needsBinding()) {
$fn = $fn->bindTo($o);
}
(Or something.)
That said, it sounds like you're talking about something different anyway that
is probably unfeasible.
Also, please do not top post.
--Larry Garfield
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php