TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   __call (http://www.talkphp.com/advanced-php-programming/4109-__call.html)

Tanax 04-06-2009 09:11 PM

__call
 
Hi!

I'm intrigued(spelling?) how this really works.
I have it set up like this:

In my DB object
PHP Code:

public function __call($function$params)
    {
        
        if(
is_object($this->DB_Helper))
        {
            
            return 
$this->DB_Helper->$function($params[0]);
            
        }
        
        
TANAXIA_CORE::error('DB_Helper is not loaded. Check out database config file.');
        return 
false;
        
    } 

And then my DB_helper objects' functions looks like this(for example):
PHP Code:

public function order_by($order$how)
    {
        
        
$this->order_by[$order] = $how;
        
        return 
$this;
        
    } 

But if I call it like $db->order_by('test_cat', 'DESC')
that gets on ONE line, so when it calls the function the function will be:

$this->DB_Helper->order_by('test_cat, DESC');

Leaving the second option out.. how can I get it to actually set the two parameters(or even more parameters) ??

Salathe 04-06-2009 09:42 PM

You could use call_user_func_array.

Tanax 04-06-2009 10:54 PM

Thanks! You mean I should use that in the __call function?
PHP Code:

public function __call($function$params)
{
// split the params so I get an array $array
call_user_func_array($this->DB_Helper->$function$array); 

Like that?
Is this the most efficiant/best way to do this?

Enfernikus 04-06-2009 11:01 PM

PHP Code:


return call_user_func_array(array($this->DB_Helper,$function),$params); 


Tanax 04-07-2009 08:59 PM

Worked like a charm.
Thanks alot!!


All times are GMT. The time now is 04:49 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0