0

I'm trying to see if there's a way to create functions from array values.

I have an array of database table names:

Array
(
    [0] => table1
    [1] => table2
    [2] => table3
    [3] => table4
    [4] => table5
)

etc... I want to be able to produce a function with each of the table names:

public function table1()
{
    // function code here...
}

public function table2()
{
    // function code here...
}

The code in each function will be the same for now. Just trying to get the function created every time a new table is added to the database.

Thanks!

Each function will look something like this when done:

public function tablename()
{
    $obj = new obj();
    $obj->set_table('tablename');

    $this->_output_view($obj->render());
}
6
  • Is this going to be inside of a class? Commented Dec 4, 2014 at 18:44
  • Also, what do you want each of these to do? Once you dynamically create each function, what do you want its body to be? Making them all have the same body is possible... making them all be different is harder (if even possible). Commented Dec 4, 2014 at 18:45
  • 1
    Don't know what you are trying to achieve. Perhaps call_user_func is the right for you. Commented Dec 4, 2014 at 18:47
  • What exactly are you trying to achieve. If this is in a class, maybe you can use function __call() instead of actually trying to create functions. Commented Dec 4, 2014 at 18:50
  • 1
    It seems you are skipping the whole concept of a model for some reason? Perhaps look into a good ORM like Propel or Doctrine Commented Dec 4, 2014 at 19:06

1 Answer 1

1

If this is inside of a class, then you can use the __call() "magic method" in PHP. Basically, in a class, this is called whenever you do $obj->func(), regardless of whether the function exists or not.

So, you can just do the action needed without creating functions.

Here's a simple example:

class Tables{
    private $tables;

    function __construct(){
        // Your array of tables
        $this->tables = array(
            'table1',
            'table2',
            'table3'
        );
    }

    // This is automatically called when doing $yourObj->table1(), for example
    function __call($name, $params=array()){
        // Check if the method called is a table name
        if(in_array($name, $this->tables)){
            // If so, then do whatever with the table name
            $obj = new obj();
            $obj->set_table($name);

            $this->_output_view($obj->render());
        }
        // Let's make sure to pass on the call to other methods in this class
        elseif(method_exists($this, $name)){
            return call_user_func_array(array($this, $name), $params);
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Rocket. That's pretty much what I was looking for. The framework I'm using has some weird issue with the __call method but they have their own _remap() which basically does the same thing. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.