Closed ryomo closed 8 months ago
There is no difference in the calling method from Phalcon 5.4, so I don't understand why it will work in 5.4. However, since method_exists() checks for existence regardless of whether it is a static method, implementations that rely on it may not be appropriate. However, I think it is an overkill to use the ReflectionFunction class to make judgments. So I'm reluctant to call it a bug.
On the other hand, I think it is inappropriate to define a "static" method with a name that is confusingly similar to a regular getter.
@s-ohnishi
There is no difference in the calling method from Phalcon 5.4, so I don't understand why it will work in 5.4.
In 5.4.0, toArray()
didn't call getters.
However, I think it is an overkill to use the ReflectionFunction class to make judgments. So I'm reluctant to call it a bug.
Agree with you. Most appropriate issue template was "bug". The others were "feature" and "security vulnerability".
On the other hand, I think it is inappropriate to define a "static" method with a name that is confusingly similar to a regular getter.
I mostly agree with that.
But static getters are commonly used. ref
Model->property
and Model->toArray()
having different behaviors cause confusion.
In 5.4.0,
toArray()
didn't call getters.
I'm sorry. It seems I was watching an unintended version.
Model->property and Model->toArray() having different behaviors cause confusion.
toArray()
also has an option that does not use a getter, so if you want to collect raw data, you can specify that.
The role of the getter is to retrieve the data after making it readable, formatting, and other adjustments, rather than the raw data, so I think it makes sense to retrieve it through it by default.
And again, I think what's causing the confusion is that you gave the "static" method the same name as the regular getter.
In the Phalcon model, getProperty()
is reserved for getters, so you should think that you cannot define a method with the same name (static or not).
But static getters are commonly used.
This is a case where the conditions of use are permitted, and it does not apply in all cases.
And again, I think what's causing the confusion is that you gave the "static" method the same name as the regular getter. In the Phalcon model, getProperty() is reserved for getters, so you should think that you cannot define a method with the same name (static or not).
Really? Is it reserved?
My thought was like below.
Phalcon's get~
is just using PHP's __get()
.
And PHP's __get()
"will not be triggered in static context." ref
So, we wrote some static getters to retrieve data like static findFirstBy~()
methods.
Anyway, there are lots of workaround, and this problem is an edge case.
Here are my workarounds.
Rename the static getter. Easy but it doesn't apply to my situation.
Override toArray param.
public function toArray($columns = null, $useGetter = false): array
{
return parent::toArray($columns, $useGetter);
}
Override toArray.
Most of lines are from phalcon toArray and added !(new ReflectionMethod($this, $method))->isStatic()
.
So, we wrote some static getters to retrieve data like static
findFirstBy~()
methods.
I think the confusion is caused by giving the method the same name as a regular getter (for example, getProperty()
).
I think there is a problem with names that cannot be inferred, such as that it is a static method or that it returns a value after identifying it with a specified key.
From these things, I think there is a mistake in the way it is used, not a bug on Phalcon's side.
It may be necessary to refactor as the framework advances. Even if there are many changes, I would choose this option.
It is up to you to override existing methods with the default useGetter flag set to false. That kind of usage is common. I think it's unnecessary, but that's just my personal opinion.
Normally, you would think that the role of toArray()
is to obtain the properties held by that instance.
Therefore, it would be overkill to prepare a countermeasure in case you create a static method with a misleading name.
Describe the bug
If I use
Model->property
, staticget~()
functions are not invoked. ButModel->toArray()
callsget~()
functions even if they are static.To Reproduce
Steps to reproduce the behavior:
test.php:
terminal:
Expected behavior
Details