First of all, this has nothing to do with the function, it has to do with her return, or more precisely with how she’s manipulating that return. You can do this, but you should also treat the function return appropriately. If you received a array shall print each element of it individually as any array, you are trying to print the array as a whole, so it just shows that it is a array, if you want each element to print through a loop, something like this:
foreach($casa->getTitleIdLocation() as $item) {
echo $item;
}
Anyway you have a function call error array()
. How are you calling her with $this
, PHP thinks that this function belongs to its object, which is not the case, so take it off. It may be that’s what you’re most concerned about, but it’s not the biggest mistake, it’s just the most visible.
Or else you don’t want to result one array, who knows wants a data in text, something like that:
function getTitleIdLocation(){
return $title . " " . $id . " " . $location;
}
I put in the Github for future reference.
But it may not even be appropriate, because it brings the information formatted in a way that may not be ideal for your case and it will be tempting to even manipulate this text to format otherwise, ie a gambit to fix the wrong concept.
Or you may just want to return the items individually and use them the way you want. Then most people today would create a method called getter for each class member. It is a possibility, but this is questioned by some because a class should not have methods with defined specific function or others will say that access to the member directly is not as serious as some people say it is (yes, people follow cake recipes without questioning why they are doing it). Especially in PHP this doesn’t make much sense, but people use it because someone read a Java book and started to disclose that in PHP it had to be the same. A lot of PHP programmers adopted this without questioning if it made sense, I won’t say because since people get angry when I tell the real reason.
What is the return you would like in
getTitleIdLocation
? What should be$this->array
?– Woss
I would like the getTitleIdLocation function to print title, id and Location values.
– DanielWD
Did the answer solve your question? Do you think you can accept it? See [tour] if you don’t know how you do it. This would help a lot to indicate that the solution was useful for you. You can also vote on any question or answer you find useful on the entire site (when you have 15 points).
– Maniero