An interesting example are configuration variables.
For example, suppose a scenario where services are consumed, or a basis of approval, or production. These services originate through a URL, which varies depending on the basis in question.
Therefore, listed Urls for these services can be determined by a flag that determines whether the application is in production mode or type approval. See the following example:
// variavel que define ambiente - homogação ou produção
$isHomolog = true;
// constantes para ambiente de homologação
const URL_SERVICO_1_HOMOLOG = "http://homolog1.com";
const URL_SERVICO_2_HOMOLOG = "http://homolog2.com";
// constantes para ambiente de produção
const URL_SERVICO_1_PRODUCTION = "http://production1.com";
const URL_SERVICO_2_PRODUCTION = "http://production2.com";
// constantes utilizadas para os serviços
const URL_SERVICO_1 = $isHomolog ? URL_SERVICO_1_HOMOLOG : URL_SERVICO_1_PRODUCTION;
const URL_SERVICO_2 = $isHomolog ? URL_SERVICO_2_HOMOLOG : URL_SERVICO_2_PRODUCTION;
In this example, constants are defined for the type-approval and production environment. In addition, the variable $isHomolog
identifies whether the environment is type-approval (value true
) or output (value false
). Hence, the service variables used by the system URL_SERVICO_1
and URL_SERVICO_2
has its value conditioned to environment variable $isHomolog
.
The advantage of this mechanism is that there is no need to exchange several Urls in the system, depending on its environment: type approval or production.
Note that, the same behavior can-be obtained with variables that are not constant, however the creation of constants ensures that they are not modified by the application. Note that, this possibility of condition in your creation is not allowed after the constants are defined, which ensures their correct use by the system.
Another interesting case is the concatenation of Strings to create a constant. in previous versions of PHP, when trying to create Strings constants based on parts of other Strings, the following message is shown: "PHP Parse error". Now, this is allowed. This is exemplified in link shown in the question itself. See an example below:
// Constant declarations
const PHP = "PHP";
const LOBBY = "Lobby";
const PHPLOBBY = PHP . " " . LOBBY;
echo PHPLOBBY . "\n";
echo "\n=====================\n\n";
Note that the PHPLOBBY constant is defined by a concatenation of Strings. This is useful when constants are used in the system, but so is the concatenation of the system. In order to make the code cleaner, a constant being the result of the concatenation of several times is very useful. This type of assignment for constants is already possible, both in Java and in C# . NET.
Complicated answer why own Feature is already almost a antipattern. Constants are... hell, constant, immutable. From the moment you, programmer, use variables, make calculations, change programmatically, is no longer constant. So much for implementing and people shoot themselves in the foot like that...
– Bruno Augusto
@Brunoaugusto You can imagine the worst-case scenario using the new functionality?
– gpupo
In my opinion there is no worst-case scenario because they are all bad. The example you presented may even make some sense, but every didactic example, in a sense, makes sense. But in the real world, in real Applications I see this gambiarra.
– Bruno Augusto
@Brunoaugusto Constant scalar Expressions looks a lot like what is used in
variables.less
bootstrap. The purpose of my question is to understand how Constant scalar Expressions is used in other languages to define when we will use this tool correctly. So far I agree with you and hope to work a little more on this theme– gpupo
It would be good, then, to highlight this particular question (about other languages) because it got a little lost in the middle of the topic.
– Bruno Augusto
@Brunoaugusto, what is your opinion on the amendment of the question?
– gpupo
See if the new wording was good. I dried up enough to focus more on the concept and technique than on the implementation and use. Anything, just edit again and complement something pertinent that might have been cut in excess (I don’t think).
– Bruno Augusto
@Brunoaugusto Let’s see how the answers look from the question as it is. Thank you!
– gpupo
I agree with @Brunoaugusto, I do not think this Feature is something so interesting in terms of applicability and a new utility, but yes, one more way to do something that was already possible in php, I followed this question trying to be convinced otherwise, but so far no.
– Marcelo Aymone
Thanks for the @Marceloaymone review. So far, I see the use of functionality where more than one occurrence of the same default value is used in more than 2 method parameters in the same object.
– gpupo
What happened to the examples of the question?
– gmsantos
@gmsantos, Brunoaugusto proposed a new formatting of the question, simplifying it. What you found ?
– gpupo
@Brunoaugusto, what is your assessment of the response given by neoprofit?
– gpupo
@Marceloaymone , what is his assessment of the response given by neoprofit?
– gpupo
Honestly, it was not a bad answer, but it lacked at least one practical (and best written) example for both scenarios, good and bad. Mainly for the misuse, since that App::() was a little out of context for me.
– Bruno Augusto