1

I have the the following code in a script that I am learning from and I tried echoing or printing the variable thinking it is an array but apparently that doesn't work.

The full code goes something like this

$i = new b();

$i->c = "d";
$i->e = "f";
$i->g = "h";
$i->j = "k";
$i->l = "m";

I have tried echoing and printing the variable $i but thinking it might be an array but it does not work, while returning a Fatal Error saying that the class 'b' was not found.

Dan Burton
  • 53,238
  • 27
  • 117
  • 198
Simon Suh
  • 10,599
  • 25
  • 86
  • 110
  • http://www.php.net/manual/en/language.oop5.php – Ignacio Vazquez-Abrams Dec 20 '10 at 06:21
  • Related: http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php – deceze Dec 20 '10 at 06:27
  • 1
    +1 while not an interesting question, it's a well described learner question, and attempts to solve yourself have been made. But give `var_dump($i)` or `print_r()` a try next time when `echo` leads to an error or no output. – mario Dec 20 '10 at 06:36
  • @stereofrog: Reading it again, I think it might not actually have. He said there was a Fatal error. But that was caused by the non-existent class. So the echo never executed I suppose. -- That being said, I managed to cause a neverending loop of death with echo once. – mario Dec 20 '10 at 10:03

3 Answers3

3

The new b() part creates a new instance of class b. It would be an object, not an array, if a class named b was defined elsewhere in your source code. The other lines assign some strings to properties of that object.

You can read more about object oriented programming in PHP in the manual.

You might find the var_dump function useful in the future.

Dan Grossman
  • 51,866
  • 10
  • 112
  • 101
2

This code instantiates a new object $i from a (apparently non-existent) class b and sets several object properties.

Please read the introduction to Object Oriented Programming for more.

deceze
  • 510,633
  • 85
  • 743
  • 889
1
  • $i = new b();     Instantiate a new object named $i from class b
  • $i->c = "d";       Assign object $i's member c the string value "d"
Babiker
  • 18,300
  • 28
  • 78
  • 125