Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7415243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:15:48+00:00 2026-05-29T07:15:48+00:00

I am making a library for CodeIgniter, and I wish to pass multiple parameters

  • 0

I am making a library for CodeIgniter, and I wish to pass multiple parameters of different types (a PDO object, username and password, configurations, etc).

I know I can pass an array of all of these things, but that doesn’t seem to be the best way of doing things (as $params can’t ever describe what is needed).

How can I pass multiple parameters to a library?

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T07:15:49+00:00Added an answer on May 29, 2026 at 7:15 am

    There are several approaches to this particular problem. I’ll list (in preferred order) ways I know to solve it:

    Associative Array Arguments:

    This approach is pretty flexible, as the order of the parameters doesn’t matter, and it resolves a pretty big complaint many have with how PHP defines function parameters. You simply pass in the “non-default” parameters you want. This is probably the most “codeigniterish” way to do it, if that’s even a thing.

    class MyLibrary {
    
        public function __construct($params = array())
        {
            // Merge default parameter names and values,
            // with given $params array
            $params = array_merge(array(
                'server'   => 'myserver',
                'database' => 'mydatabase',
                'username' => 'myuser',
                'password' => 'mypassword'
            ), $params);
    
            // Create variables from parameter list
            extract($params);
    
            var_dump($server);
            var_dump($database);
            var_dump($username);
            var_dump($password);
        }
    
    }
    
    // Initialization:
    $this->load->library('mylibrary', array(
        'server'   => 'server-arg1',
        'database' => 'database-arg2'
    ));
    

    Numbered Arguments:

    This approach replicates the typical PHP parameter paradigm (defines names, orders, and default values for all expected parameters).

    class MyLibrary {
    
        public function __construct($params = array())
        {
            // Add relevant defaults to missing parameters
            $params = array_merge($params, array_slice(array(
                'myserver',
                'mydatabase',
                'myuser',
                'mypassword'
            ), count($params)));
    
            // Create variables from parameter list
            extract(array_combine(array(
                'server',
                'database',
                'username',
                'password'
            ), $params));
    
            var_dump($server);
            var_dump($database);
            var_dump($username);
            var_dump($password);
        }
    
    }
    
    // Initialization:
    $this->load->library('mylibrary', array('server-arg1', 'database-arg2'));
    

    Override the CI Loader class:

    This is AT YOUR OWN RISK. Basically, the CI_Loader::_ci_init_class() method needs to be overridden with a MY_Loader class and corresponding method. These are the lines that you “don’t like” (lines 1003-1012 in my install):

        // Instantiate the class
        $CI =& get_instance();
        if ($config !== NULL)
        {
            $CI->$classvar = new $name($config);
        }
        else
        {
            $CI->$classvar = new $name;
        }
    

    The “safest” replacement that I could guess would be this:

        // Instantiate the class
        $CI =& get_instance();
        if (isset($config[1])
        {
            // With numeric keys, it makes sense to assume this is
            // is an ordered parameter list
            $rc = new ReflectionClass($name);
            $CI->$classvar = $rc->newInstanceArgs($config);
        }
        elseif ($config !== NULL)
        {
            // Otherwise, the default CI approach is probably good
            $CI->$classvar = new $name($config);
        }
        else
        {
            // With no parameters, it's moot
            $CI->$classvar = new $name;
        }
    

    I really don’t know how many things this will break, but I can almost certainly say there will be something. It’s not really worth the risk. I’d STRONGLY recommend the first approach above.

    Cheers!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble simply making multiple pages with CodeIgniter. For example, I am
I'm making a c++ library thats going to be P/Invoked from c#, so i
Hello i am making an static library for distribution. I am distributing the .h
I am trying to use the flXHR javascript library for making cross-domain calls. I
While making some final tests of a class-library that I'm writing for Windows Mobile
Help I am busy making changes to a type library in a Datasnap project.
I'm trying to learn the code igniter library and object oriented PHP in general
i tried making library with ar -r -c -s libtestlib.a *.o as given in
I'm making a server library in which the packet association is done by enum.
I am basically making a video library where you download videos and I then

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.