1

I have the code below. For some reason when I run this code it say the variable rsLinks is undefined. Yet clearly you can see that it is defined in the constructor. Can you tell me what I am doing wrong?

 require_once "..\Models\Links.php";

class Navigator
{
    public $rsLinks;

    public function __construct() 
    { 
        $rsLinks = new rsLinks();
    }

    public function getLinks()
    {
        $links = $rsLinks;
    }
}

$navigator = new Navigator();
$navigator->getLinks();
1
  • I expect this has something to do with scoping. Commented May 17, 2012 at 6:16

4 Answers 4

5

Use $this->rsLinks; instead.

Sign up to request clarification or add additional context in comments.

2 Comments

Oh thank you. Now I feel dumb. Thanks Odinn so quickly as well!
I personally hate the fact that you have to use $this in PHP... :) you welcome.
1
public function getLinks()
{
    //$links = $rsLinks;
    $links = $this->rsLinks;
}

Comments

0

$rsLinks is a regular local variable with function scope.
To access object properties, use $this->rsLinks.

Comments

0

Php is not Java. You need to use $this.

$this->rsLink

On both cases.

1 Comment

You speak the truth. I keep forgetting that.

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.