Update: the problem is related to compilation, if I compile with disable-all then the code works as expected. Strangely, only post 5.3 versions are affected. I will recompile the latest version a number of time, adding just one --enable-* at a time to find out what causes the problem and then will post the result.
I'm trying to migrate to php 5.5 and can't find a way round the simple thing, discribed many times but in different context.
I'm getting
"Warning: Creating default object from empty value"
and I know that the workaround is to use stdObject, but it just doesn't work for me. Looks like I made a mistake somewhere, please help me.
I made a following code:
class MyClass {
public $param;
public function MyClass() {
$this->param = "Param initialized";
}
}
$myClass = new MyClass();
echo $myClass->param."
";
It gives me an empty line. I tried to add $this = new stdClass();
before param initialization, but it just hides the warning, "param" is still uninitialized.
I also tried to define constructor as __construct
which gives absolutely the same result.
All I need is a simple example of object constructor which would initialize any attribute of the object being initialized.
Any ideas?
Update: Guys, this is the real code. First, desirable behavior with 5.3:
-bash-4.1$ cat tc.php
<?
class MyClass {
public $param;
public function MyClass() {
echo "in Constructor
";
$this->param = "Param initialized";
}
public function getParam() {
echo "in getParam
";
return $this->param;
}
}
$myClass = new MyClass();
echo "1: ".$myClass->param."
";
echo "2: ".$myClass->getParam()."
";
?>
-bash-4.1$ ../php-5.3.29/sapi/cli/php tc.php
in Constructor
1: Param initialized
in getParam
2: Param initialized
-bash-4.1$
Now same code with 5.5 (same with 5.6):
-bash-4.1$ ../php-5.5.30/sapi/cli/php tc.php
in Constructor
Warning: Creating default object from empty value in tc.php on line 9
1:
in getParam
2:
Now code corrected to avoid warning:
-bash-4.1$ cat /export/home/csweb/CS/Activiti-Decta/CScripter/tc.php
<?
class MyClass {
public $param;
public function MyClass() {
echo "in Constructor
";
$this = new stdClass();
$this->param = "Param initialized";
}
public function getParam() {
echo "in getParam
";
return $this->param;
}
}
$myClass = new MyClass();
echo "1: ".$myClass->param."
";
echo "2: ".$myClass->getParam()."
";
?>
-bash-4.1$ ../php-5.5.30/sapi/cli/php tc.php
in Constructor
1:
in getParam
2:
And for sake of completance php 5.6
-bash-4.1$ ../php-5.6.14/bin/php tc.php
in Constructor
1:
in getParam
2:
Any suggestion how to get same results as in 5.3 with 5.5 or 5.6?