简体   繁体   中英

Codeigniter pass params to function

I'm new to OOP and I'm having some trouble on understanding the structures behind it. I've created a library in Codeigniter (Template), which I pass some parameters when loading it, but I want to pass those parameters to the functions of the library.

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Template {

    public function __construct($params)
    {
        echo '<pre>'; print_r($params); echo '</pre>';
        //these are the parameters I need. I've printed them and everything seems fine
    }

    public function some_function()
    {
        //I need the above parameters here
    }

}

Try this:

class Template {

    // Set some defaults here if you want
    public $config = array(
        'item1'  =>  'default_value_1',
        'item2'  =>  'default_value_2',
    );
    // Or don't
    // public $config = array();

    // Set a NULL default value in case we want to use defaults
    public function __construct($params = NULL)
    {
        // Loop through params and override defaults
        if ($params)
        {
            foreach ($params as $key => $value)
            {
                $this->config[$key] = $value;
            }
        }
    }

    public function some_function()
    {
        //i need the above parameters here

        // Here you go
        echo $this->config['item1'];
    }

}

This would turn array('item1' => 'value1', 'item2' => 'value2'); into something you can use like $this->config['item1'] . You are just assigning the array to the class variable $config . You could also loop through the variables and validate or alter them if you wish.

If you don't want to override the defaults you set, just don't set the item in your $params array. Use as many different variables and values as you want, it's up to you :)

As Austin has wisely advised make sure to read up on php.net and experiment yourself. The docs can be confusing because they give a lot of edge case examples, but if you check out the Libraries in Codeigniter you can see some examples or how class properties are used. It's really bread-and-butter stuff that you must be familiar with to get anywhere.

Make class members like this:

class Template {
    var $param1
    var $param2    

    public function __construct($params)
    {
        $this->param1 = $params[1]
        $this->param2 = $params[2]
        //and so on
    }
}

Then you can use them in your function

You may want to store the parameters as properties in your class so all your methods will have access to them.

See this documentation about properties in PHP 5: http://www.php.net/manual/en/language.oop5.properties.php

EDIT: Actually, if you're completely new to OOP, you'll find that it can be difficult to wrap your head around at first. Asking questions on SO one at a time as you run into problems will be a very inefficient way to go about it. If you want to save some time, I would recommend starting by reading a basic text that explains the concepts of OOP separate from language-specific implementation details (eg The Object-Oriented Thought Process ). Then, when you want details, the PHP docs on the subject are pretty good (and free).

I would recommend deciding weather the variables of the class are private or public. This helps greatly with the readability. Private variables should be used for internal variables where as public variables should be used for things that are attributes of the object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM