tags:

views:

114

answers:

7
<?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:~$ 
+14  A: 

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

return $this->contents
Extrakun
What Extrakun means is that you don't include the $ when setting or accessing an object's variables.
Lachlan McDonald
the assignment as well.
roe
There's actually another problem, too... the echo statement requires a dollar sign before the abhilash variable name.
Narcissus
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'
Extrakun
@Narcissus: Ooops! My bad, that got lost when I added the formatting..
roe
ok i understood, thanks......
abhilashm86
+4  A: 

If i recall correctly it would be

$this->contents = $contents;

not

$this->$contents = $contents;
Psytronic
+3  A: 

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

Simon
A: 

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

appusajeev
+1  A: 

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

Softnux
+5  A: 

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();
VolkerK
+1 I was about to write the same... I was too slow.
Felix Kling
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
@Tom: You can set contents at runtime via `$a = new abhi('new content')`
Felix Kling
@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.
VolkerK
A: 

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

?>
Radu