Welcome Guest, Not a member yet? Register   Sign In
variable as default parameter in function?
#1

[eluser]gscharlemann[/eluser]
I'm reading some data from a config file into an array. Is there a way to do this (I think php is barfing on my default parameter assignment)

Code:
class Tracker_model extends Model
{
    public $tables = array();

    public $schedule_sort_by = array();


    public function __construct()
    {
        parent::Model();
        $this->load->config('tracker');
        $this->tables = $this->config->item('tables');
        $this->schedule_sort_by = $this->config->item('schedule_sort_by');
    }

    public function get_user_schedule($user_id, $sort_by = $schedule_sort_by['scheduled_date'])
    {
          ...
    }
}
$schedule_sort_by['scheduled_date] is the "default" column heading I would like to sort by if it's not specified. I could hard code it, but the column name might change based on setup, that's why it's defined in the config file.

thanks!
#2

[eluser]mddd[/eluser]
I don't think that it is possible to use variables in your default values.
Because the value of those variables could vary depending on the scope from where the function is called.
However, you can easily resolve this by doing:
Code:
public function get_user_schedule($user_id, $sort_by = NULL)
{
if (is_null($sort_by)) $sort_by = $this->schedule_sort_by;
}
#3

[eluser]gscharlemann[/eluser]
good call. thank you




Theme © iAndrew 2016 - Forum software by © MyBB