4

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

I have reduced this error down to the smallest amount of code and it is so small, that I really feel stupid here.

I have 2 files class1.php :

  1 <?php
  2 class insertText{
  3         //properties
  4         protected $paragraph1='<p>this is a test paragraph</p>';
  5         protected $bigHeader='<h1>header1</h>';
  6         protected $textArray;
  7
  8         public function __construct () {
  9                 $this->$textArray = array($this->$bigHeader1, $this->$paragraph1);
 10         }
 11         public function getText(){
 12                 return $this->$textArray;
 13         }
 14 }
 15 ?>

and test.php:

1 <?php
2 include './class1.php';
3 echo "Begin\n";
4
5 $filler = new insertText();
6 echo $filler->getText();
7
8 echo "end\n";
9 ?>

When I run: $ php text.php

Begin
PHP Notice:  Undefined variable: bigHeader1 in class1.php on line 9
PHP Fatal error:  Cannot access empty property in class1.php on line 9

I know that this has te be an easy answer, and I did search and search every post with either error message. I even tried moving the assignment of text to the variables to within the constructor and it dodn't work.

Community
  • 1
  • 1
Dano
  • 53
  • 1
  • 5

1 Answers1

6

You need to use:

$this->textArray = array($this->bigHeader1, $this->paragraph1);

...instead of..

$this->$textArray = array($this->$bigHeader1, $this->$paragraph1);

i.e.: The second dollar symbol in $this->$bigHeader1 is redundant. (You're in effect attempting to access a class element that would be defined within the $bigHeader1 variable, as opposed to the class instance variable itself.)

John Parker
  • 54,048
  • 11
  • 129
  • 129
  • Oh man, I know it had to be a stupid answer. Sorry to waste your time. – Dano Jun 20 '11 at 21:23
  • @Dano Don't worry about it - we all make such mistakes from time to time. :-) – John Parker Jun 20 '11 at 21:25
  • as well, your protected var is `$bigHeader`, but you're referring to it in the constructor as `$bigHeader1` (note the `1`). – Marc B Jun 20 '11 at 21:29
  • aside from the fact that I am trying to echo an array (has been changed to print_r :) ) I am still getting the same error messages. here is the code `code` this is a test paragraph'; protected $bigHeader1='

    header1'; protected $textArray; public function __construct () { $this->$textArray=array($this->bigHeader1,$this->paragraph1); } public function getText(){ return $this->$textArray; } } ?>

    – Dano Jun 20 '11 at 21:35
  • You've still got `$this->$textArray`, which is incorrect. Should be `$this->textArray` (no `$` on textArray) – Marc B Jun 20 '11 at 21:38
  • ok it is working. Thanks. One more question how do you get the block of code to show up on the mini markup? I tried to put `code` before and after my code, but that didn't work, I tried `my code in between the backticks` and that didn't either edit: hahah that worked in the example I just wrote above.... oh well – Dano Jun 20 '11 at 21:41
  • @Dano Backticks are just for small code snippets - if you intend the code with 4 spaces, it'll automatically convert a block. (The { } editor button does this for you.) :-) – John Parker Jun 21 '11 at 20:29