【问题标题】:Creating default object from empty value从空值创建默认对象
【发布时间】:2011-01-12 15:17:14
【问题描述】:

我需要在运行时设置对象变量,它正在工作,但是 PHP 会返回我:

从空值创建默认对象 (/sct/fw FW_List.class.php:1142)

function initTemplates(&$object, $tpl_names)
{

 global $FW_LIST_CONFIGURATION;
     $is_custom = array();

     foreach($tpl_names as $tpl_type) {
        $object->$tpl_type =  new template();
        $object->$tpl_type->setTemplateText($FW_LIST_CONFIGURATION["templates"][$tpl_type]);
            // place the defauts template values
        $object->$tpl_type->setPlace( $FW_LIST_CONFIGURATION["css"][$tpl_type]);
  }
  return $is_custom;
}

ps。我不能使用$object = new stdClass,因为$object 是一个指针。

【问题讨论】:

  • 1. PHP 不支持指针,那么$object 怎么可能是指针呢? 2. 由于PHP5默认所有对象都是通过引用传递的,所以你应该省略&。 3. 你使用了未定义的变量$stdObj 和$$FW_LIST_CONFIGURATION。
  • 对不起,我的代码是这样的:$object->$tpl_type 所以我写到这里,我错过了提问的时间:\
  • 我编辑了问题,将原始函数放在我的代码中。
  • 错误是这一行:$object->$tpl_type = new template();帮助!

标签: php object default stdclass


【解决方案1】:

解决了,秘诀就是使用数组;

$object->test[$tpl_type] =  new template();
$object->$tpl_type = $object->test[$tpl_type];
$object->$tpl_type->setTemplateText($FW_LIST_CONFIGURATION["templates"][$tpl_type]);

【讨论】:

    猜你喜欢
    • 2020-04-09
    • 2015-01-28
    • 2017-11-29
    • 2017-09-30
    • 2016-08-18
    • 2018-07-18
    • 2019-03-14
    • 1970-01-01
    相关资源
    最近更新 更多