2

这是一个非常简单的问题,似乎没有直接在 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 会影响最初在类头中声明的变量吗?本质上,它会在班级内是全球性的吗?

任何帮助表示赞赏。

4

2 回答 2

3

不,不会。你总是需要$this->告诉 PHP 你说的是类属性,而不是局部变量。

// Always assignment of a local variable.
$p_id = $stmt->fetch();

// Always assignment of a class property.
$this->p_id = $stmt->fetch();
于 2012-09-18T03:42:02.707 回答
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
于 2012-09-18T03:45:05.350 回答