PHP: dynamic default value for functions' parameters

default, function, parameters, php

Solution

I think it is not possible this way. But you can render a static default value to it, and in the first line, if the value is unchanged by the caller, you just assign, the dynamic value.

public function get_single_db_entry($uid, $table=0) {
    if $table == 0 
      $table = Config::$credentials["mysql"]["table"]
    // logic here...
}

A bit workaround, but it should work.

Problem

I would like to keep options in a Config class, so I don't need to change several (identical) values when changing (e.g.) MySQL database. So far I'm accessing these options like: ``` Config::$credentials["mysql"]["username"] ``` now, I would like to set a "dynamic" default value for a function, but PHP won't let me do something like ``` public function get_single_db_entry($uid, $table=Config::$credentials["mysql"]["table"]) { // logic here... } ``` What I would like to know: Is there any chance to keep default parameters' values dynamic?

Original source