private function teste()
{
echo 'teste';
}
This method just prints something does not return value, so the following line makes sense
$variavel = $this->teste();
You must change the test method to the following way
private function teste()
{
return 'teste';
}
And in relation to memory waste you would be mistaken because once the method or function reaches the first Return or at the end of your body, all the variables that are in your scope would be released from memory.
Variables declared within a function are called local variables of the function. To learn more search for the scope of variables.
According to the update of the question description, you always assign a value to a variable when you intend to reuse it more than once, or also to make the code more readable.
Assigning a variable only to give it more readability wouldn’t be memory waste, because as I said the variables will only be in memory as long as the context in which it is declared is running (example a function or method).
If it were a few decades ago, the answer would be yes due to the limitations of the hardware, but today this is no problem, the readability of the code is more important.
Its function
teste
does not return anything, and even if one returns for example the string 'test', the variable inside the function would soon be released from memory once the function was over, consequently would have no problem with memory waste.– Skywalker
I ended up getting confused. The correct is to give a Return and not echo in the test function.
– Gustavo Piucco