2

This is a very straightforward question that doesn't appear to be directly addressed at php.com - at least from looking through that section.

In any case, I have a class here with a specific function:

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();
        }

    }

Just ignore the fact that there is no constructor posted with missing functions, etc. They're in this class - just not pertinent to my question.

Will setting $p_id in the last statement affect the variable declared initially in the header of the class? Essentially, will it be global within the class?

Any help is appreciated.

2 Answers 2

3

Nope, it won't. You always need $this-> to tell PHP you're talking about the class properties, not local variables.

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

// Always assignment of a class property.
$this->p_id = $stmt->fetch();
Sign up to request clarification or add additional context in comments.

1 Comment

Ha, didn't even catch that even though I did that for every other variable. Well, that answered it as well as fixed a future issue. Thanks.
0

No. That's a local variable to your function. If you did $this->$p_id = 'blah'; then it would affect it. The variable you have defined in your class is a property, so it has to be accessed/altered with $this->...., whereas the variable you have in your function, is just a local variable (which you can play with by simply doing $p_id='....' ).

So,

$this->$p_id = '';//will alter the class property

and

$p_id = '';//will alter the local var defined/used in the function

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.