Welcome Guest, Not a member yet? Register   Sign In
Dynamic Member Functions in CodeIgniter
#1

[eluser]Davvolun[/eluser]
Okay I'd like to believe this will be interesting to others...

Let's say I have a MySQL database with a lot of tables, and I'd like to provide the basic scaffolding for each of them. One way I could do that would be to create a function for each table that redirects to the proper url i.e.

Code:
class Welcome extends Controller {
  function Welcome() {
    parent::Controller();
    $this->load->helper('url');

    $tables = $this->db->list_tables();
    foreach( $tables as $table ) {
      $this->load->scaffolding( $table );
    }
  }
  ...
  function proxyFunction( $tableName ) {
    //ehh I think this is right, close enough for the example
    redirect( 'welcome/' . $tableName . '/scaffolding/' );
  }
  ...
  function tableNameOne() {
    proxyFunction( 'tableNameOne' );
  }
  function tableNameTwo() {
    proxyFunction( 'tableNameTwo' );
  }
  ...
}

Problem is I need to create a function for each table (replicating the database structure), and if the database changes (say, on early prototyping), then I need to change the controller also.

Does anyone know if I can do something like this (the following code does not work, but can it be changed to work???):

Code:
class Welcome extends Controller {
  function Welcome() {
    parent::Controller();
    $this->load->helper('url');

    $tables = $this->db->list_tables();
    foreach( $tables as $table ) {
      $this->load->scaffolding( $table );
      
      //new code
      $lcTable = strtolower($table);
      $this->$lcTable = &$this->proxyFunction; //this line fails to work as hoped

      $this->$lcTable(); // <----- causes a PHP error
    }
  }
  ...
  function proxyFunction() {
    $table = basename(uri_string());
    redirect( "welcome/" . $table . "/scaffolding/" );
  }
  ...
}

I can use a member variable as a work-around, such as

Code:
class Welcome extends Controller {
  private $dynamicFunctions = array();
  ...
  function Welcome() {
    ...
    foreach( $tables as $table ) {
      $this->load->scaffolding( $table );
      
      //new code
      $lcTable = strtolower($table);
      $this->{$this->dynamicFunctions[$lcTable]} = &$this->proxyFunction;

      $this->{$this->dynamicFunctions[$lcTable]}();
    }
  }
  ...
}

But CodeIgniter won't find the proxied function through the member variable now. Additionally, I can use __get and __set, but then CI can't find stuff it needs (if anyone thinks the last example is a worthy area of pursuit, I'll post my code for that).

Note: obviously in each of these I'm just trying to see if the PHP code was valid by calling the proxied function; in a real application, I'd remove that and use the URL with CI calling the function.

Solutions? Worthy problem for discussion? Airspeed velocity of an unladen swallow?


Messages In This Thread
Dynamic Member Functions in CodeIgniter - by El Forum - 05-15-2009, 12:40 PM
Dynamic Member Functions in CodeIgniter - by El Forum - 05-15-2009, 12:45 PM
Dynamic Member Functions in CodeIgniter - by El Forum - 05-15-2009, 01:00 PM
Dynamic Member Functions in CodeIgniter - by El Forum - 05-15-2009, 03:21 PM



Theme © iAndrew 2016 - Forum software by © MyBB