New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The functionality of the __callStatic
magic method is incomplete.
#13813
Comments
|
Right. This is consistent with the fact that two functions (static and non-static) cannot coexist, if declared directly. |
Not correct, please check manual. All dynamic methods are inaccessible in a static context. |
In static context, public methods are inaccessible. |
That's not how it currently works. Static and instance methods share the same symbol table. You can even call static methods as instance methods. As such, both Your interpretation is completely reasonable, but not how it works. PHP is an implementation specified language, and any such change would require an RFC. See https://wiki.php.net/rfc/howto on how this process works. |
Reminder that
|
I understand the explanation, but I believe the outcome should be the same. When a static call is made in a static context without an instance, it should invoke the |
And that's quite reasonable. Dare I say it even makes sense, especially if you look at how other languages work (even though there are significant differences between what compiled and interpreted languages can do...). But it's not compatible with the philosophy of how PHP currently does methods and method calls, and changing that philosophy to fit this pattern could be a huge break to anyone relying on the current behaviors. Which is why iluuu1994 marked this as Requires RFC: we're not saying "we cannot and will not do this" and most certainly not "you are wrong and this is bad", but instead we're saying "changing this is a big and complicated thing to talk about, and these GitHub Issues are just not a good enough medium for us to give it the conversation it deserves". Like, the first step in the RFC process is actually to (re)start the conversation on the internals mailing list - that being where all the super cool dev people hang out... |
Thank you for your kind response. I will prepare well and raise my voice again through the RFC process. |
@daddyofsky just make your function |
@brzuchal If there were no __callStatic magic method, of course, I would have used it statically. Do you find it strange to use it like MyClass::privateMethod()? You might not like it, but you would think it's possible because there is __callStatic. I think the same. With __callStatic existing, why can't it work for public methods? That's what I'm questioning. Moreover, using MyClass::privateMethod() like this doesn't actually employ it statically. Inside __callStatic, it creates a singleton or an instance of another class. Yes, that's correct. I want to use public methods in the same way, like Laravel's ORM. |
I don't share your opinion on it. I think differently, the "privateMethod" is meant to be called from the inner scope of the class and nowhere else, so it doesn't exist for the outer - public scope. Seems logical that __callStatic is applied here.
Because __callStatic was designed to be called whenever the method doesn't exist in callee scope and this is a clearly defined behavior since always. |
What you pointed out is correct. In the current behavior of __callStatic, it is as you say. However, there is one aspect that is being overlooked. That is, it's __callStatic not __call. This means that in the static scope, even public methods can be considered non-existent. I am interested in this aspect, and I think that with a slight change to the design of __callStatic, it could become an even more powerful magic method. Perhaps the difference in opinions arose because the issue was initially registered as a bug. If that's the case, I apologize. This issue has already been changed from a bug to a Thank you for your interest. |
AFAIK there is no such thing as "static scope", but if you find it in the PHP Manual I'd be happy to read about it.
No worries, I didn't consider this as a bug at all. I understand your reasoning, I only don't consider it correct. |
__call() is triggered when invoking inaccessible methods in an object context. __callStatic() is triggered when invoking inaccessible methods in a static context. The manual uses the terms "object context" and "static context." Your mention of "variable scope" corresponds to "object context," and my mention of "static scope" refers to what is meant by "static context." |
Description
The following code:
Resulted in this output:
But I expected this output instead:
It seems that the
__callStatic
magic method is discriminating against public methods. ;)PHP Version
PHP 8.x
Operating System
Any
The text was updated successfully, but these errors were encountered: