【发布时间】:2025-12-28 22:10:06
【问题描述】:
class A{
public $name;
public function __construct() {
$this->name = 'first';
}
public function test1(){
if(!empty($_POST["name"]))
{
$name = 'second';
}
echo $name;
}
$f = new A;
$f->test1();
我们为什么不得到first 以及如何为A 类设置正确的默认值变量$name?
如果有任何帮助,我将不胜感激。
【问题讨论】:
-
$this->name和$name是两个不同的变量... -
@deceze 我知道,但是如何在类中设置打印
$name的变量的默认值是first?如果 makepublic $name; $this->name='first';不会打印first。 -
我是说你需要在
test1()函数中使用$this->name而不是$name。另一部分很好,您可以将其恢复为初始版本。 -
@deceze 好吧,我理解你,我理解这是如何工作的。谢谢!