class blog_lib{ var $CI; var $posts_path; var $file_ext='.md'; var $_all_posts; var $_all_tags; var $_all_pockets; public function __construct($pathselect) { if (!isset($this->CI)) { $this->CI =& get_instance(); } $this->posts_path = FCPATH.$pathselect; }
Controller:
public function index(){ $this->load->library('blog_lib');$data['config'] = $this->blog_config;$this->load->library('twig_lib'); $data['all_tags'] = $this->blog_lib->get_posts_tags(); $data['posts'] = $this->blog_lib->get_posts();$this->twig_lib->render("index.html",$data); }
How do I pass arguments to the constructor?
Reply content:
class blog_lib{ var $CI; var $posts_path; var $file_ext='.md'; var $_all_posts; var $_all_tags; var $_all_pockets; public function __construct($pathselect) { if (!isset($this->CI)) { $this->CI =& get_instance(); } $this->posts_path = FCPATH.$pathselect; }
Controller:
public function index(){ $this->load->library('blog_lib');$data['config'] = $this->blog_config;$this->load->library('twig_lib'); $data['all_tags'] = $this->blog_lib->get_posts_tags(); $data['posts'] = $this->blog_lib->get_posts();$this->twig_lib->render("index.html",$data); }
How do I pass arguments to the constructor?
A CI is a parameter that can be passed to a class's constructor, but must be an array when the constructor accepts arguments:
class blog_lib{ ... public function __construct($param){ ... $this->posts_path = FCPATH.$param['pathselect']; } ...}
Controller Inside:
$this->load->library('blog_lib',array('pathselect'=> 'xx'));
If you think this way is not good can be in another way, is in the CI's custom class to make a virtual empty class, and then after the empty class to define a real class, by config/autoload.php automatically load this class, you can new, then how to pass all can.
application/library/blog.php:
//虚拟类class blog{}// 真实类class blog_lib { ....}
config/autoload.php
$autoload['libraries'] = array('blog');
Controller Inside:
$oblog = new blog_lib($value);