简体   繁体   中英

PHP OOP question

I have a syntax question about PHP OOP.

I have two functions.

public function setBody($body) {   
  $this->body = $body;
 }

public function return_value($value) {   
  return $value;
 }

$body is declared higher up in the function. if I want to call the return_value function (which I realize does nothing -- it's just an example for me to use to learn) on setBody, what's the syntax for that?

I tried $this->body = return_value($body) and it didn't work. I also tried return_value($this->body) as a second line as well, and it didn't work either.

Thanks in advance for your help.

You'll need to do this:

public function setBody($body) {   
  $this->body = $this->return_value($body);
  // Make a reference to the object you are dealing with -- return_body
  // is not a global function -- it's a method of the object you are creating.
}

public function return_value($value) {   
  return $value;
}

You use the $this-> notation on both the property and the method, like this:

public function setBody($body) {
    $this->body = $this->return_value($body);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM