Search code examples
phparraysfunctionparameters

Functions in Arrays,


I'm making a PHP class that will store functions in an array so you can override the functions anywhere. The problem I'm facing is that the functions accepts X number of parameters but when I call them it sends an array containing the parameters.

This is the class

<?php
class Controller
{
    /**
     * array functions
     * 
     * It will contain the functions that will be used by the controller.
     * The key of each index is a name that describes the function, don't change
     * them, if you want to make a plugin, just keep same name and add a diferent
     * function.
     * 
     * The value is the function itself, it will accept an array as parameter
     * that will contain the arguments, whether you want to be the array, I don't
     * just make it works
     */
    private $functions;
    
    /**
     * Constructor
     * 
     * This will set the default indexes in the $functions array, you don't need
     * to change anything here, this are the default functions of Alexya's core
     */
    public function __construct()
    {
        $this->functions = array(
            /**
             * This is an example, it's never used
             * 
             * You can add the functions directly here or use Controller::set()
             * which accepts the key of the index as first parameter (a string)
             * and the value of the index as second parameter (a function)
             * 
             * To call this function just use Controller::load() which accepts
             * a string as first parameter (in this case "test") and an array
             * as second parameter (which will be the parameters of the function)
             */
            "test" => function($params) {
                var_dump($params);
            },
            
            /**
             * Register function
             *
             * @see clases/Session.php
             */
            "register_user" => "Session::register"
        );
    }
    
    /**
     * Adds an entry to the array
     * 
     * Use this method if you want to add a function that Alexya will execute
     * Use the default names if you want to override an existing function or
     * use your own if not.
     * 
     * @param string name name of the function that will be saved in the array
     * @param function function the function that will be executed
     * 
     * @return true if $name already exists and was overwrited, false if it didn't
     *         exist but was added properly
     */
    public function __set($name, $function)
    {
        if(array_key_exists($name, $this->functions)) {
            $this->functions[$name] = $function;
            return true;
        } else {
            $this->functions[$name] = $function;
            return false;
        }
    }
    
    /**
     * Executes a function
     * 
     * This method will execute a function of the array.
     * 
     * @param string name name of the function to execute
     * @param mixed param parameters that will be passed to the function
     * 
     * @returns true if functions exists false if not
     */
    public function __call($name, $param)
    {
        if(!array_key_exists($name, $this->functions)) {
            if(DEBUG) {
                echo "Call to undefined function $name in Controller class!";
            }
            return false;
        }
        
        return call_user_func($this->functions[$name], $param);
    }
}

And this is how I call it:

<?php
//include Alexya's Core
require_once("globConfig.php");

//Redirect user if he cant access the page
$Controller->user_can_access_website();

echo "<br/>";

$Controller->test(array(1,2,3,4,5,6,7,8,9));

echo "<br/>";

$Controller->register_user("asdfasdf", "asdfasdf", "[email protected]");

The first call to Controller is ok, as the function check_user_has_access doesn't exist in the array shows an error.

The next call, $Controller->test, dumps an array containing an array that contains 1-9.

And the last call sends an array containing the given parameters. However, the function that is called doesn't accept an array but those 3 parameters.

Is there any way I can fix it?

This is the function that I'm calling:

/**
     * Performs a register attempt
     * 
     * Will try to perform a register attempt with the given username and password
     * if the register succed user will be redirected to home page
     * 
     * @param string username user's name
     * @param string password text password (will be encrypted here)
     * @param string mail register email
     * 
     * @return false if register failed
     */
    public function register($username, $password, $email)
    {
        global $Database;
        global $Alexya;
        
        /**
         * Can continue, boolean
         *
         * If an error happened this flag will be switched to false
         */
        $can_continue = true;
        
        //check username
        if(empty($username)) {
            Results::addFlash(array(
                        "result" => "error",
                        "message" => "Username can't be empty!"
                    ));
            $can_continue = false;
        } else if(strlen($username) < $Alexya->min_username_length) {
            Results::addFlash(array(
                        "result" => "error",
                        "message" => "Username can't be shorter than $Alexya->min_username_length characters!"
                    ));
            $can_continue = false;
        } else if(strlen($username) > $Alexya->max_username_length) {
            Results::addFlash(array(
                        "result" => "error",
                        "message" => "Username can't be longer than $Alexya->max_username_length characters!"
                    ));
            $can_continue = false;
        }
        
        //check password
        if(empty($password)) {
            Results::addFlash(array(
                        "result" => "error",
                        "message" => "Password can't be empty!"
                    ));
            $can_continue = false;
        } else if(strlen($password) < $Alexya->min_password_length) {
            Results::addFlash(array(
                        "result" => "error",
                        "message" => "Password can't be shorter than $Alexya->min_password_length characters!"
                    ));
            $can_continue = false;
        } else if(strlen($password) > $Alexya->max_password_lenght) {
            Result::addFlash(array(
                        "result" => "error",
                        "message" => "Password can\'t be longer than $Alexya->max_password_lenght characters!"
                    ));
            $can_continue = false;
        }
        
        //check email
        if(empty($email)) {
            Results::addFlash(array(
                        "result" => "error",
                        "message" => "Email can't be empty!"
                    ));
            $can_continue = false;
        } else if(preg_match("", $email)) {
            Results::addFlash(array(
                        "result" => "error",
                        "message" => "The email see,s to be incorrect!"
                    ));
            $can_continue = false;
        }
        
        //Check no error ocurred
        if($can_continue) {
            $password = md5($password);
            
            //check username/pass exists
            $username_exists = $Database->query("SELECT * FROM accounts WHERE username='$username'");
            
            if($username_exists && $username_exists->num_rows == 0) {
                $sessionID = $Controller->generate_sessionID();
                
                //insert user in database
                $userID = $Database->insert("users", array(
                                            "username"  => $username,
                                            "password"  => $password,
                                            "email"     => $email,
                                            "sessionID" => $sessionID
                                        ));
                
                if(is_numeric($userID)) {
                    $_SESSION["sessionID"] = $sessionID;
                    Results::addFlash(array(
                                "result" => "success",
                                "message" => "You're now registered!"
                            ));
                    Functions::redirect(URL."?page=home");
                } else {
                    Results::addFlash(array(
                                "result" => "error",
                                "message" => "Couldn't add user to database!"
                            ));
                }
            } else {
                Results::addFlash(array(
                            "result" => "error",
                            "message" => "Wrong username/password, please try again!"
                        ));
            }
        }
        
        return false;
    }

EDIT: this is the output:

Call to undefined function user_can_access_website in Controller class!<br />
<b>Warning</b>:  Missing argument 2 for Session::register() in <b>/home/cabox/workspace/classes/Session.php</b> on line <b>126</b><br />
<br />
<b>Warning</b>:  Missing argument 3 for Session::register() in <b>/home/cabox/workspace/classes/Session.php</b> on line <b>126</b><br />
<br />
<b>Warning</b>:  strlen() expects parameter 1 to be string, array given in <b>/home/cabox/workspace/classes/Session.php</b> on line <b>145</b><br />
<br />
<b>Warning</b>:  strlen() expects parameter 1 to be string, array given in <b>/home/cabox/workspace/classes/Session.php</b> on line <b>151</b><br />
<br />
array(2) {
  [0]=>
  string(4) "test"
  [1]=>
  array(1) {
    [0]=>
    array(9) {
      [0]=>
      int(1)
      [1]=>
      int(2)
      [2]=>
      int(3)
      [3]=>
      int(4)
      [4]=>
      int(5)
      [5]=>
      int(6)
      [6]=>
      int(7)
      [7]=>
      int(8)
      [8]=>
      int(9)
    }
  }
}

EDIT 2: Using the function call_user_func_array and adding some debuggin lines, this is what I have while calling "test" function:

__call function:
    name = test
    parameters: 
array(1) {
  [0]=>
  array(9) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
    int(3)
    [3]=>
    int(4)
    [4]=>
    int(5)
    [5]=>
    int(6)
    [6]=>
    int(7)
    [7]=>
    int(8)
    [8]=>
    int(9)
  }
}
test function: string(4) "test"

As you can see, now it doesn't send an array containing the parameters, it now sends the first parameter from the array sended in __call function


Solution

  • Ok I've found the problem.

    The code that calls the function was this:

    /**
     * Executes a function
     * 
     * This method will execute a function of the array.
     * 
     * @param string name name of the function to execute
     * @param mixed param parameters that will be passed to the function
     * 
     * @returns true if functions exists false if not
     */
    public function __call($name, $param)
    {
        if(!array_key_exists($name, $this->functions)) {
            if(DEBUG) {
                echo "Call to undefined function $name in Controller class!";
            }
            return false;
        }
    
        return call_user_func($this->functions[$name], $param);
    }
    

    The line

    return call_user_func($this->functions[$name], $param);
    

    Was sending an array as parameter, that array contained the given parameters from the __call function.

    To fix this @deceze told me to use call_user_func_array(string, array) function instead. My mistake was that I was using it like this:

    return call_user_func_array($this->functions[$name], func_get_args());
    

    The code was sending the parameters of the magic method __call instead of the parameters given in the array $param. To fix this I changed func_get_args() for $param so the line would look something like this:

    return call_user_func_array($this->functions[$name], $param);
    

    Hope someone finds it usefull