-
Notifications
You must be signed in to change notification settings - Fork 19
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
Do not allow the usage of magic properties on WP_Post objects #229
Comments
Regarding the concern about IDEs and code analyzers, there is a PHPDoc syntax (
... which actually offers more documentation and type hinting than inline What are the main arguments against magic properties in the PHP community? Did a bit of searching and I didn't see anything written about it. Personally, I think allowing access to post properties through magic getters in specific contexts (like template files) allows for readable and expressive code, and the ergonomic savings of not writing out |
Where would this documentation be added? For the properties that are part of the This wouldn't cover post meta though, since the First once you retrieve the same post in two different places, you would have to copy over the documentation. Secondly once a new meta field is added, one would need to ensure that the documentation is updated. Which is hard to do even if there is just a single instance. Because developers first would need to know that they have to do this, and they would need to know where to do this.
Not sure about the type hinting, since a meta value retrieved with the
Due to the limitations of PHPCS, it's hard to restrict rules to certain type of files. So it's either allowed everywhere, or not allowed everywhere.
WordPress has implemented these magic getters because its classes don't use object oriented programming principles. Getters and setters have always been the norm in PHP, although there are exceptions of course. I agree that getter method calls are more expressive and readable, but using magic getters is using the wrong tool for the job. If you want to have a nicer interface, with type hinting benefits, then use business objects. These are classes that tie together custom post types, post meta, terms, etc. into a consistent interface that speaks business language, rather than low level implementation language. Such an approach was implemented on TechCrunch, see the Event class, and the associated factory for such types. With the new more detailed documentation for arrays, and argument and return type hinting, this can be really powerful. |
I agree with Fränk main problem for me is that it's bypassing filters. For example: the If you need to have the direct access then there are ways to silence the coding standards but the default way should be to use the filter points. The other arguments are less relevant than that. |
Just for clarity, I don't think getting meta values through the magic getter bypasses any filters - it's just a syntactic sugar for calling |
The
WP_Post
class offers access to various properties through magic getters:$page_template
$ancestors
$post_category
$tag_input
In addition, post meta can be retrieved by using
$post->meta_key
.We should not allow the usage of these coding approaches:
$post->meta_key
does not always return the meta data.sanitize_post_field()
before being returned.$single
set totrue
.All in all there are no benefits here, and only downsides.
The text was updated successfully, but these errors were encountered: