I've a following code snippet written in a file titled "prog_1.php":
<?php
ini_set('display_startup_errors',1);
ini_set('display_errors',1);
error_reporting(-1);
class SimpleClass {
// property declaration
public $var = 'a default value';
// method declaration
public function displayVar() {
echo $this->var;
}
}
$instance = new SimpleClass();
$assigned = $instance;
$reference =& $instance;
var_dump($instance);
echo "\n";
echo $reference; //At this step I'm getting a "Catchable fatal error"
echo "\n";
var_dump($reference);
?>
My first issue is with the below line from above code :
echo $reference;
For this code line I'm getting following error :
**Catchable fatal error:** Object of class SimpleClass could not be converted to string in /var/www/practice/prog_1.php on line 23
I'm not understanding if I could var_dump()
the object of a class then why can't I echo the same?
My second doubt is if execute following two statements :
var_dump($instance);
var_dump($reference);
Both the statements given me the same output as follows :
object(SimpleClass)#1 (1) {
["var"]=>
string(15) "a default value"
}
Why so? Isn't there any difference between object and reference? What does this #1
mean in above output?
Hope someone could clear my above doubts by providing me satisfactory and best in class answers in a simple, easy to understand and crispy language.
Thanks in advance.