I can’t access the function

Asked

Viewed 93 times

-2

I’m trying to access the function getTitleIdLocation() in order to display the values I gave to the three variables, but I cannot. I cannot call the array in this way?

Code:

<?php

class Imovel {

private $title;
private $id;
private $location;


 function __construct($title, $id, $location){
     $this->title = $title;
     $this->id = $id;
     $this->location = $location;
 }

 function getTitleIdLocation(){
     return $this->array($title, $id, $location);


 }
}

$casa = new Imovel ("Apartamento", "3", "Porto");
echo $casa -> getTitleIdLocation();
?>
  • What is the return you would like in getTitleIdLocation? What should be $this->array?

  • I would like the getTitleIdLocation function to print title, id and Location values.

  • 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).

1 answer

1

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.

  • Thanks for the answer! My goal is to even print out all the elements of the array as you said. I put the foreach to print one element at a time, but it still didn’t work.

  • Then you have some other problem in a new code, so you have to ask another question, this question I answered.

  • Anyone who’s negative could say what’s wrong for me to fix, I hate to keep getting the wrong answer. If you don’t have that information, it’s probably just a retaliatory vote.

  • Just so we’re clear... it wasn’t me! must be someone who did not understand the problem or found something in his reply that was unclear (as cited retaliation, I do not have this aspect because different opinions for the reason of the closure, does not believe to do this in the answer that is right)

  • @Virgilionovic is very strange, because this question clearly has a problem in which he calls a PHP function as if it were the object and I say that this is a problem and then he has the print array, When he wants to print the elements, I even speculated on some options. This answer https://answall.com/q/400513/101 received 2 positive on something that should not even be able to post because it was closed, and he speculates what he wants to do, which if that were the case the question would be out of scope because he wants you to write the whole topic,but it is not clear why the error ñ has been described

  • The mistake is this: Coloquei o foreach para imprimir um elemento de cada vez, mas mesmo assim nao funcionou., just isn’t saying what the real mistake is, and there also isn’t so it can’t be answered properly. So the vote is pretty weird.

  • Unfortunately yes the vote is strange .

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.