From all of the topics, I covered at my "When PHP Framework Sucks" series, this one maybe annoys me the most. Not because I have a particular problem with style or standards for annotations and configurations than because I'm unable to reuse code and expect the same behavior with any other environment.
What I meant when I say "framework magic"? I prefer to complain about these four magic points:
- Defining code inside method or class annotations
- Defining classes instances in yaml or any file different than .php
- All config inside yaml, XML, or any other file
- Containers which "contains" anything
Defining code inside method or class annotations
We tend to use annotations so often for defining method properties. I recently saw complete documentation for swagger in controller above every action. My god, it was so long that I wasn't able to fit it in my screen height. The notation which exceeds screen hight! That's a red alarm of first grade. Long notations are not just ugly, they create so much noise, that prevents you from seeing the actual code.
It is also very common to see validation inside notation. I disagree with that so much. First, if you change the library for validation, reuse your code somewhere else, validation library becomes deprecated or similar, you will end up with the useless string above your method.
Instead, use some Value Object for validating specific data types, try to enforce strong types in every method.
/**
*@library Iβm the useless piece of comment if you change the library hehehehe
*/
public function someMethod()
{
}
Defining classes instances in yaml or any file different than .php
Classes instances in non-PHP files are very problematic if you want to follow the execution line in your IDE and then you end up that some class is a dead end. This class is not a dead end as it seems to be, there is a code which instantiates it, but it is in yaml.
Wait, YAML ???
True, many time I see the declaration for classes which we have to define as a "service" for example, inside XML or yaml files.
All config inside yaml, XML, or any other file
Generally, config inside yaml files annoys me. I know it brings some readability for folks, but I always wonder, why not simply in php? No need to parse, no need to warm up.
Containers which "contains" anything
This one is wrong, at least from two perspectives. Perspective number one, the container return type is what? Containers can return on get method call (or similar name) any type, any object, or property. How do I know that in the name of God? How can I call any method on that object since I don't know its type?
public function testAction()
{
$something = $this->container->get('hehehehe_you_newer_know_who_I_am');
}
The second problem is that container violates such a hard "Interface segregation principle" the famous "I" in SOLID principles. Your Controller or any other class which has access to the container knows too many things at this point.
Conclusion
Notations are comments, you can use it to specify some method properties, but do it with caution. If you try to find the usage of the class, defined in yaml, good luck! There is unnecessary parsing of yaml and XML configs. Controllers and other container aware objects know too much, but you don't have an idea what container returns.
Top comments (5)
The reason to not put config information inside PHP is because it makes the application useless when deploying to Heroku, ElasticBeanstalk, CloudFoundry, or any other large-scale cloud service. These cloud providers need to be able to pass critical information to the app such as which port to bind a listener to, or the current URL of the attached database instance, and so forth. Everything else is spot on though on here, nice work.
Hi Kenneth,
Thank you very much for this point I find it very valuable, and I will definitely use it as the only exception for putting configs outside of PHP code. On the other hand, I complained as well that yaml or XML files are used for defining relations between object, as well as annotations.
Thank you once again for spotting this.
Hi Damjan, I agree 100% about not defining classes or relations inside XML or YAML files, or any other file other than a .php file. I don't have much experience with frameworks that work this way, but it would probably drive me crazy to have to work in that manner.
Hi!
I work with two PHP frameworks:
Symfony, not by choice.
CakePHP by choice.
While the first one has every one of the problems you listed here, the second one is the exact opposite. Everything is PHP, you don't have any xml or yaml configuration files.
Saludos,
I've seen the container pattern used in Symfony as a way to allow circular dependencies. If A requires B and B requires A, then one of those needs to require a container and resolve what it needs when it needs it, instead of in constructor.