I've been a professional C, Perl, PHP and Python developer.
I'm an ex-sysadmin from the late 20th century.
These days I do more Javascript and CSS and whatnot, and promote UX and accessibility.
If you know that the $properties array only contains elements which are suitable for pushing onto the $i object you could go with Alex Cioroianu's suggestion.
In this way the acceptable fields are listed only inside the class that define them, and the properties could also be protected or private (* see below). And the code of the OP could simply be:
I do really like this approach!
I wasn't aware you can assign property names like this and creating a static class is so much cleaner and provides a lot of benefits.
This is what I was looking for :)
Can you please explain what : self
exactly is used for?
From what I understand it allows you to create an instance of the class inside the static method but it works also when I remove it.
I don't really know how to search for it and couldn't find an explanation.
It's a PHP 7 feature, it's called "return type declaration": basically you declare that the function will always return something having the declared type; if you return something else, like an integer, or don't return anything, a TypeError is thrown. I really like this feature, strict type force you to write better code.
This is great, guess most of the time I use PHP in a too "simple" way and should get more familiar with such details as it really helps to improve and ease the code :)
It's basically like in C++, or similar languages, when you define a function with a specific return type like
PHP7's null coalesce operator is awesome, thank you for pointing this out.
I like the other options more for the purpose of my question but this is a great "upgrade" from the inline if.
I do like Alex Cioroianu suggestion as I don't really need to assign a value if it does not exist but this is still helpful if I need to assign some fallback value or similar.
Cheers
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
One way to make this shorter is to use PHP7's null coalesce operator:
and to make it a bit DRYer, you could loop through the properties:
If you know that the
$properties
array only contains elements which are suitable for pushing onto the$i
object you could go with Alex Cioroianu's suggestion.Personally I would move that logic inside a QualityIssue static method:
In this way the acceptable fields are listed only inside the class that define them, and the properties could also be protected or private (* see below). And the code of the OP could simply be:
(*) because yes, you can access a private property from outside the object, if you do it from a method of that class:
I do really like this approach!
I wasn't aware you can assign property names like this and creating a static class is so much cleaner and provides a lot of benefits.
This is what I was looking for :)
Can you please explain what
: self
exactly is used for?
From what I understand it allows you to create an instance of the class inside the static method but it works also when I remove it.
I don't really know how to search for it and couldn't find an explanation.
Cheers
It's a PHP 7 feature, it's called "return type declaration": basically you declare that the function will always return something having the declared type; if you return something else, like an integer, or don't return anything, a TypeError is thrown. I really like this feature, strict type force you to write better code.
This is great, guess most of the time I use PHP in a too "simple" way and should get more familiar with such details as it really helps to improve and ease the code :)
It's basically like in C++, or similar languages, when you define a function with a specific return type like
Thanks for the explanation, I struggled to find a good search query for this question with only the code.
PHP7's null coalesce operator is awesome, thank you for pointing this out.
I like the other options more for the purpose of my question but this is a great "upgrade" from the inline if.
I do like Alex Cioroianu suggestion as I don't really need to assign a value if it does not exist but this is still helpful if I need to assign some fallback value or similar.
Cheers