Codeigniter库类的公共变量不起作用

时间:2012-05-23 14:51:33

标签: php class codeigniter variables

以下是我的代码片段:

<?php

if (!defined('BASEPATH')) {
exit('No direct script access allowed');
}

class sample{

function __construct() {
    $this->ci = & get_instance();
    }

public $name;
public $style;


function set_data($data)
{
    /* List of parameters that you can set */

    $this->name = (isset($data['name']) ? $data['name']: ''); // Set select name
    $this->style = (isset($data['style']) ? $data['style']: ''); // Set select style

}

function select_both_dropdown()
{
    $select = '<select name="'.$this->name.'" class="chzn-select" style="'.$this->style.'">';
    $select .= '<option value=""></option>';
    $select .= '</select>';
return $select;
}

控制器:

$data['select'] = $this->sample->select_both_dropdown(array(
        'name' => 'eventselect',
        'style' => 'min-width: 247px;'
    ));

如何在控制器中加载它:     function _ construct(){         父:: _construct();         $这 - &GT;负载&GT;库( 'tank_auth');         $这 - &GT;负载&GT;库( '样本');     }

当选择加载到屏幕上时,没有任何内容被填充。没有名字,没有风格等。我做错了什么?

1 个答案:

答案 0 :(得分:2)

您需要正确引用它们;

在图书馆;

// as you have done
$this->select = 'foo';

在您的控制器中,它将类似于$this->sample->name$this->sample->style

但$ select不是属性或被返回,因此不可用。

更新 - 基于评论 控制器;

$this->sample->set_data(array(
        'name' => 'eventselect',
        'style' => 'min-width: 247px;'
    ));
$data['select'] = $this->sample->select_both_dropdown();

另一个更新

function select_both_dropdown($data)
{
    $this->set_data($data);
    $select = '<select name="'.$this->name.'" class="chzn-select" style="'.$this->style.'">';
    $select .= '<option value=""></option>';
    $select .= '</select>';
return $select;
}

然后我会将set_data()更改为private而不是public。