首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >数组中的函数,

数组中的函数,
EN

Stack Overflow用户
提问于 2015-03-15 20:23:49
回答 1查看 95关注 0票数 0

我正在制作一个PHP类,它将把函数存储在一个数组中,这样你就可以在任何地方覆盖这些函数。我面临的问题是,这些函数接受X个参数,但当我调用它们时,它会发送一个包含这些参数的数组。

这就是班级

代码语言:javascript
复制
<?php
/**
 * Controller Class
 * 
 * This class will handle the controllers, it will contain an array with the
 * functions that will handle each controller (I know it's a bad description but
 * read the code and you'll know what I mean -.- :* ). Anyway, read the code
 * I think it's big enough to make it look something cool
 * 
 * @author Manulaiko
 */
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);
    }
}

我是这样称呼它的:

代码语言:javascript
复制
<?php
/**
 * Index page
 * 
 * This file will contain the behaivour of the site, everything will be here
 * 
 * @author Manulaiko
 */

//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", "asdfasdf@g.as");

第一次调用Controller是正常的,因为数组中不存在函数check_user_has_access会显示一个错误。

下一个调用$Controller->test将转储一个数组,该数组包含一个包含1-9的数组。

最后一个调用发送一个包含给定参数的数组。但是,所调用的函数不接受数组,而接受这3个参数。

有什么办法可以修好它吗?

这是我要调用的函数:

代码语言:javascript
复制
/**
     * 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;
    }

编辑:这是输出:

代码语言:javascript
复制
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)
    }
  }
}

编辑2:使用函数call_user_func_array并添加一些调试行,这是我在调用“测试”函数时得到的:

代码语言:javascript
复制
__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"

正如您所看到的,现在它不会发送包含参数的数组,而是发送在__call函数中发送的数组中的第一个参数

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29060562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档