这是一个非常简单的问题,似乎没有直接在php.com上解决 - 至少从浏览该部分开始。
无论如何,我在这里有一个具有特定功能的课程:
class CheckOut extends DB_MySQL{
public $fName;
public $lName;
public $numberOut;
public $p_id;
/.../
protected function publisherCheck($lName, $fName)
{
$this->lName = $lName;
$this->fName = $fName;
//Execute test
$this->checkConnect();
$stmt = $this->dbh->prepare("SELECT p_id FROM People WHERE lastName = :param1 AND firstName = :param2");
$stmt->bindParam(':param1', $this->lName);
$stmt->bindParam(':param2', $this->fName);
$stmt->execute();
//Determine value of test
if($stmt == FALSE)
{
return FALSE;
}
else
{
$p_id = $stmt->fetch();
}
}
请忽略这样一个事实,即没有发布缺少函数的构造函数等等。他们在这个课程中 - 与我的问题无关。
在最后一个语句中设置$ p_id会影响最初在类头中声明的变量吗?从本质上讲,它会在课堂上全球化吗?
感谢任何帮助。
答案 0 :(得分:3)
不,不会。你总是需要$this->
告诉PHP你在谈论类属性,而不是局部变量。
// Always assignment of a local variable.
$p_id = $stmt->fetch();
// Always assignment of a class property.
$this->p_id = $stmt->fetch();
答案 1 :(得分:0)
没有。这是你的功能的局部变量。如果你做$this->$p_id = 'blah';
那么它会影响它。你在类中定义的变量是一个属性,所以它必须用$this->....
来访问/改变,而你函数中的变量只是一个局部变量(你可以通过简单的操作来玩) $p_id='....'
)。
所以,
$this->$p_id = '';//will alter the class property
和
$p_id = '';//will alter the local var defined/used in the function