How does the CI framework pass parameters to a constructor in a custom class through $this->load->library?

Source: Internet
Author: User
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);
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.