0

In the following code I am getting error: Undefined variable: base_url on line 27 and Cannot access empty property on line 27.

I need to echo the parameter http://google.com passed in get_url() from render().

<?php

$smart_url = new smart();

$smart_url -> get_url('http://google.com');

echo  $smart_url -> render();


 /**
 * Smart URL
 */

class smart {

private $base_url;

/**
 * 
 * @param string $url
 */
public function get_url($url) {
        $this -> $base_url = $url;

}


/**
 * @return string $base_url
 */
public function render() {
    return $this -> $base_url;


}

}


?>
1
  • This is a common PHP newbies mistake. Don't vote down on the question. And just btw as much is it asked as much easier it will be for newcomers to find their mistake. I asked a similar question myself an year ago. :D Commented May 19, 2012 at 7:27

4 Answers 4

1

It's just a syntax error. You access your parameters this way:

 $this->base_url;

Without the $before the parameters' name

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

Comments

1

Syntactically incorrect...
$this -> $base_url = $url;

Should be

$this->base_url = $url;

Comments

1

Short answer:

Use: $this->base_url

Long answer:

When you are accessing properties you don't need the dollar sign. Access them simply as that:

$this->base_url

The second one would access a class property with the name $base_url (the content of the variable).

This is a common mistake. Use the dollar sign only for declaring variables in this context:

protected $base_url;

Comments

1

$this -> $base_url is wrong; no $ ,$this->base_url

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.