This course is archived!
This tutorial uses Symfony 3, but all the concepts around PHP 7 are still ?valid.
03.
Return Types
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
9 Comments
Hello.
Please, check the current video. For me it doesn't play :(
Hello! Vimeo, the service we use to host our videos, was having an outage issue earlier today. It now seems to be resolved and videos are playing again (yay!). Please let me know if you are continuing to have issues accessing content.
On declaring strict types: Instead of PhpStorm I used Netbeans 11.1 for an IDE, including the php-cs-fixer plugin. If I include the declare(strict_types=1); statement in, say, a controller class as above, I get a "hint" that doing so is a PSR-1 violation. I am not sufficiently fluent in PSR to know whether that hint is correct. Is it? [The first class I tried contains only a namespace, several use statements and a class with two methods, each returning a Response object.]
Hey GeoB,
Well, if it's just a tip - you can completely ignore it. If you want to deal with it deeper - I'd recommend you you look at the PSR-1 standard then: https://www.php-fig.org/psr... - PSR-1 is just a basic coding standard, so it might be so that you just do not have a required empty line etc, in other words it depends on your actual code, so difficult to say for sure for me because I do not use NetBeans lately.
I hope this helps!
Cheers!
there Thank you so much for yet another great post. In regards to type hinting in php 7, sometimes in our entity we don't want a variable to accept null - and when using the form component, it will trigger an error exactly like this:
https://stackoverflow.com/q...
The proposed answer suggests to always use a Data Transfer Object (DTO), and reading the sources mentioned, they suggest an entity should not be in invalid state. May I ask your opinion about this? Not sure what you think I don't think this is feasable as it would be very difficult to handle DTOs with OneToMany relationships for example, as one would have to map the entity and all its relations to the DTO - and for something like an update, I can't see how this would be realistically doable - isn't that just plainly impossible to use with Doctrine ORM and Symfony form component?
So do you guys at KNPU personally use DTO (which would solve the type hinting issue)? Using DTO seems impossible to update entities with OneToMany relationship for example, or am I missing something.. Thank you!
Hey Anthony R.!
GREAT question :). First, by design, the form component does put your objects into invalid state: that's just the nature of how it works, for better or worse. So, you're right about that :). Second, no, we rarely use DTO's on KnpU. And when we do, it's not for this reason: we use DTO when we have a form that is significantly different than any entities. In that case, using a DTO is just a matter of simplifying our code :). We're definitely more on the pragmatic side of things, and so, our entities usually do allow things to be null. And, I personally don't have a problem with that: if it's important for business for something to NOT be null, I make sure the field is setup as nullable=false in the database so that it IS caught when persisting.
Overall, the issue is that it's so common for our entities to need to be in an invalid state. For example, if you have a User class with 6 required properties, then the moment you say
$user = new User()it's invalid - the 6 "required" properties are null. The only true way to make your entities always valid is to put any required properties as constructor arguments so that they are never null ($user = new User('weaverryan', 'Ryan', 'Weaver', 'ryan@knpuniversity.com', true, 'ROLE_USER')). I really don't like this :). So, that's why we "catch" any "should not be null" errors during persistence. Btw, if you DO want to create an entity that has required constructor arguments, that actually is possible with the form component - https://webmozart.io/blog/2015/09/09/value-objects-in-symfony-forms/ - but not worth it for us.I know that's not a totally satisfying answer, but I hope it makes you feel better at least ;).
Cheers!
Ryan - thank you so much for this info. It's incredibly useful & I really appreciate you always taking the time to reply to your readers. Everything makes sense and I do love the idea of being more pragmatic. Thank you!
Hello!
Can I configure the strict_types=1 in all my project? I don't want to put strict_types=1 in each file.
Is it possible?
Thanks!
Hey JuanLuisGarciaBorrego!
Yea... this is *not* possible. The reason is that, as we learn about in this tutorial, it's up to the author of each file to decide if they want to have strict_types=1 for that file. Then, they write their code accordingly. If there *were* some way to do it globally, PHP would apply it to *all* files, including third-party vendor files, which weren't expecting that behavior. And since there's no way for PHP to know *your* files versus *their* files, it all just kinda doesn't work :). So yea... annoying. My best advice - which you probably already thought of - is to update your PhpStorm (or whatever editor) to automatically add that line when you create a new PHP file / class.
Cheers!
"Houston: no signs of life"
Start the conversation!