douduan6731 2015-10-07 14:27 采纳率: 0%
浏览 62

构造函数中的对象初始化(php 5.5)

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?

  • 写回答

1条回答 默认 最新

  • dqq3623 2015-10-08 09:10
    关注

    The problem was caused by wrong EXTRA_CFLAGS passed during the php compilation. Here is the one which worked:

    export CPPFLAGS="-m64"; 
    export EXTRA_CFLAGS="-m64";
    make clean;
    ./configure --disable-all --disable-cgi --with-openssl --enable-json --with-oci8=instantclient,instantclient64_12_1/
    make
    

    And the one which produced php binary with such a strange behavior:

    EXTRA_CFLAGS="-D_XPG4_2 -D__lint -m64 -DSIOCGIFNAME"
    

    Why so many things were added to CFLAGS? Because I tried to complie with --with-sockets, which produced compilation errors, so I was trying to show gcc the way around. PHP compiled successfully and make tests shown no signs of problems.

    Don't have a time or desire to continue digging, I'm happy now as system smoothly running on 5.6

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?