开发者

CodeIgniter static class question

If I would like to have several static methods in my models so I can say User::get_registered_users() and have it do something like

public static function get_regist开发者_运维技巧ered_users()
{
    $sql = "SELECT * FROM `users` WHERE `is_registered` = 0";
    $this->db->query($sql);
    // etc...
}

Is it possible to access the $this->db object or create a new one for a static method?


It's simple:

get_instance()->db...

or

$CI =& get_instance();
$CI->db->get('users')->result();
$CI->session->set_userdata('login', TRUE);


Matt S is correct, though Kohana was built for PHP5, so previous compatibility isn't much of an issue.

Static methods do not have access to other variables. If the variable was defined as self::db you could use it then, and you might want to instantiate it that way to do it.

Static methods are best for things that don't require objects, such as formatting text, redirecting pages, etc.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜