[英]PHP OOP question
I have a syntax question about PHP OOP. 我有一个关于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. $ body在函数中声明为更高的位置。 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? 如果我想在setBody上调用return_value函数(我意识到什么也没做-这只是我用来学习的一个示例),那么它的语法是什么?
I tried $this->body = return_value($body)
and it didn't work. 我尝试了$this->body = return_value($body)
,但是没有用。 I also tried return_value($this->body)
as a second line as well, and it didn't work either. 我也尝试了return_value($this->body)
作为第二行,但是它也不起作用。
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: 在属性和方法上都使用$this->
表示法,如下所示:
public function setBody($body) {
$this->body = $this->return_value($body);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.