if I have methods like
function getVar1(){
return $this->var1;
}
function getVar2(){
return $this->var2;
}
and so on…
and these vars are meant to be printed on the screen, is it a good idea to create additional methods that directly print them?
like
function Var1(){
echo $this->getVar1();
}
function Var2(){
echo $this->getVar2();
}
or should I stick with calling echo getVar1() where I need them?
Functions should do what their name implies. It’s one of the core design principles. When you say
getVar(), it does what it’s meant to do: return a variable.In your example, you are not even specifying what the function does. If you want to have a function that prints something, then call it
printVar1()or something like that.On the other hand, I have to agree with ThiefMaster. You can just use echo
getVar1()instead of creating bigger classes. Especially in an interpreted language like PHP(where classes add more overhead than a compiled language).