phpclass Car var $color; function __construct$color = green $this-color = $color; function what_color return $this-color; echo $this-color; $a = new Car;echo $a-what_color; 为什么retu
在该代码中,函数what_color()中的return $this->color;语句会返回$this->color的值,而echo $this->color;语句则只是将$this->color的值输出到屏幕上。
在调用what_color()方法时,echo $a->what_color();语句会输出$this->color的值,因为$a->what_color()返回了$this->color的值,所以可以通过echo语句将其输出。
而echo $this->color;语句在return $this->color;之后,所以它不会被执行到。当函数遇到return语句时,它会立即结束执行并返回指定的值。所以在这种情况下,echo语句不会被执行。
原文地址: https://www.cveoy.top/t/topic/iAnx 著作权归作者所有。请勿转载和采集!