1
<?php
class abhi
{
    var $contents="default_abhi";

    function abhi($contents)
    {
        $this->$contents = $contents;
    }

    function get_whats_there()
    {
        return $this->$contents;
    }

}

$abhilash = new abhi("abhibutu");
echo $abhilash->get_whats_there();

?>

i've initialized variable contents a default and also constructor, why is the value not printing, anything i should correct here?

see the error,

abhilash@abhilash:~$ php5 pgm2.php 

Fatal error: Cannot access empty property in /home/abhilash/pgm2.php on line 13
abhilash@abhilash:~$ 

7 Answers 7

14

You are returning the variable incorrectly inside the function. It should be:

return $this->contents
Sign up to request clarification or add additional context in comments.

3 Comments

What Extrakun means is that you don't include the $ when setting or accessing an object's variables.
There's actually another problem, too... the echo statement requires a dollar sign before the abhilash variable name.
As a further explanation why it is valid syntax, you can store a function name into a variable and invoke it by putting the brackets behind it, which is why PHP complains it as an 'empty property'
5

Since the question is tagged as "php5" here's an example of your class with php5 class notation (i.e. public/protected/private instead of var, public/protected/private function, __construct() instead of classname(), ...)

class abhi {
  protected $contents="default_abhi";

  public function __construct($contents) {
    $this->contents = $contents;
  }

  public function get_whats_there() {
    return $this->contents;
  }
}

$abhilash = new abhi("abhibutu");
echo $abhilash->get_whats_there();

3 Comments

Is there any reason to re-assign contents in the constructor? I know the original poster did, but is there a value to it?
@Tom: You can set contents at runtime via $a = new abhi('new content')
@Tom: Not really in this case. But fi another class extends abhi and its constructor doesn't call abhi::__construct() $contents will at least be set to "default_abhi" which might be sufficient in some cases.
4

If i recall correctly it would be

$this->contents = $contents;

not

$this->$contents = $contents;

Comments

3

Should be accessing and writing to $this->contents not $this->$contents

Comments

1

Also, are you not missing a dollar-sign in "echo abhilash->get_whats_there();"? ($abhilash->..)

Comments

0

use $this->contents
i too at first had the same problem

Comments

0

You have a problem with $: 1. when using $this-> you do not put $ between "->" and the variable name the "$" sign, so your $this->$contents should be $this->contents. 2. in your echo youforgot the $ when calling that function from the instantiated class.

So your correct code is:

<?php
class abhi
{
    var $contents="default_abhi";

    function abhi($contents)
    {
        $this->contents = $contents;
    }

    function get_whats_there()
    {
        return $this->contents;
    }

}

$abhilash = new abhi("abhibutu");
echo $abhilash->get_whats_there();

?>

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.