PHP Reflection and Attributes for Feed Example

Publié le: 24 octobre 2025
sur la chaîne: Chris Ridings
82
3

I want to start making short videos about PHP from time to time. The idea is that they are quick to produce - so this is more me explaining my thought process as I program something how it is done in the real world and with any silly mistakes left in. Here we want to have a class representing a news item and some sort of feed system (JSON/XML). In the future there may be more classes - like a blog class.

Normally we should always program the simplest method possible. The obvious simplest method would be to have a feed class for each type (possibly inherited) and hard code in the fields. This is what AI coding would probably give you. However, it's unsatisfactory because the Feed class is not generic and more importantly because any future programmer changing classes like the news item class would need to know that they also need to edit the hard coded fields in the relevant code.

So here my argument is that you should make the trade off. Using attributes and reflection we can make a generic feed class and simultaneously make it obvious to somebody editing the news item (or future blog class) that they need to add an attribute to enable the field and they only need to work on the one file (without needing to know the other one exists). Because future work is more likely to be on the other classes, this trade off in the feed class is well worth it.


Sur cette page du site, vous pouvez voir la vidéo en ligne PHP Reflection and Attributes for Feed Example durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Chris Ridings 24 octobre 2025, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 82 fois et il a aimé 3 téléspectateurs. Bon visionnage!