Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
变量转换为另一个php文件_Php - Fatal编程技术网

变量转换为另一个php文件

变量转换为另一个php文件,php,Php,我有一个php文件(register.php),其中包含一个公共函数寄存器($data),其中验证了错误。然后计算错误,如果没有发现错误,则通过验证 register.php: class ARegister { public function register($data) { $user = $data['userData']; //validate provided data $errors = $this->validateUser(

我有一个php文件(register.php),其中包含一个
公共函数寄存器($data)
,其中验证了错误。然后计算错误,如果没有发现错误,则通过验证

register.php:

class ARegister {

    public function register($data) {
      $user = $data['userData'];

      //validate provided data
      $errors = $this->validateUser($data);

        if(count($errors) == 0) {
        //first validation
        }
    }

    public function validateUser($data, $botProtection = true) {

    $id     = $data['fieldId'];
    $user   = $data['userData'];
    $errors = array();
    $validator = new AValidator();

       if( $validator->isEmpty($user['password']) )

            $errors[] = array( 
            "id"    => $id['password'],
            "msg"   => Lang::get('password_required')
             );


      return $errors;
   }
问题是,我需要将已验证数据的确认信息发送到我的另一个php文件(othervalidation.php),在该文件中我进行了另一次验证:

othervalidation.php:

<?php
require 'register.php';

if ( !empty($action) ) {

     switch ( $action ) {

        case 'process_payment':

        try {  

            $instance = new ARegister();

            if($instance->validateUser($data, $errors)) {
            throw new Exception('Validation error');    
            }

        }   catch (Exception $e) {
            $status = false;
            $message = $e->getMessage();
        }
     }

您可以尝试以下方法:

class ARegister {

private $error = 0;

public function register($data) {

    if (!$this->validateUser($data)){

        $this->error++;
    }
}

public function getErrorCount(){
    return $this->error;
}

public resetErrorCount(){
    $this->error = 0;
}
if(empty($errors)) {
        //first validation
}
public function register(array $data) {
public function validateUser(array $data, $botProtection = true) {
if(!in_array($user['password']) or empty($user['password']))
或通过引用传递错误:

public function register(&$error, $data) {

    if (!$this->validateUser($data)){

        $error++;
    }
}
就个人而言,我会在同一个方法中(在封装类中)执行所有验证,使用错误消息参数(通过引用传递)返回验证失败的原因,并使用return语句返回true或false

class MyClass{

        public function validation(&$errorMessage, $firstParameter, $secondParameter){

            $success = false;

            if (!$this->firstValidation($firstParameter)){

                $errorMessage = "this is not working pal.";
            }
            elseif (!this->secondeValidation($firstParameter)){

                $errorMessage = "Still not working buddy...";
            }
            else{

                $success = true;
            }

            return $success;
        }

        private function firstValidation($firstParameter){

            $success = false;

            return $success;
        }

        private function secondeValidation($secondParameter){

            $success = false;

            return $success;
        }
    }
在其他文件中:

<?php
        $instance = new MyClass();

        $errorMessage = "";

        if ($instance->validation($errorMessage, $firstParameter, $secondParameter)){

            echo "Woot, it's working!!!";
        }
        else{

            echo $errorMessage;
        }
    ?>

我查看了您的新代码设计,下面是我发现的新问题

首先,在register函数中,当validate函数返回数组时,将errors变量用作整数。这里有两种可能性

如果您的错误数组为空,您可以更改您的注册方法,如下所示:

class ARegister {

private $error = 0;

public function register($data) {

    if (!$this->validateUser($data)){

        $this->error++;
    }
}

public function getErrorCount(){
    return $this->error;
}

public resetErrorCount(){
    $this->error = 0;
}
if(empty($errors)) {
        //first validation
}
public function register(array $data) {
public function validateUser(array $data, $botProtection = true) {
if(!in_array($user['password']) or empty($user['password']))
Count也是有效的,但我还是喜欢空的,因为它在语法上更清晰。此外,如果参数不是数组或可数对象,则count函数返回1;如果参数为NULL,则返回0。正如我所说的,在您当前的情况下,这是一个功能性的解决方案,但在其他一些情况下,它可能会导致您意外的结果

在您的方法声明中,我看到您需要一个布尔值(botProtection)

但您正在提供一个errors参数

if($instance->validateUser($data, $errors)) {
您没有向我提供errors变量的声明,但它可能与您的函数期望的bot保护参数不匹配。PHP使用的是丢失类型,这很有用,但再一次,您必须小心难以找到的bug。对于公共函数,您应该始终确保所提供的参数不会导致代码崩溃

在代码中,数据参数似乎是一个数组。可以使用参数提示强制使用数组,如下所示:

class ARegister {

private $error = 0;

public function register($data) {

    if (!$this->validateUser($data)){

        $this->error++;
    }
}

public function getErrorCount(){
    return $this->error;
}

public resetErrorCount(){
    $this->error = 0;
}
if(empty($errors)) {
        //first validation
}
public function register(array $data) {
public function validateUser(array $data, $botProtection = true) {
if(!in_array($user['password']) or empty($user['password']))
甚至是特定的类(就好像你在条件中使用了“instance of”)

此外,您甚至没有在validateUser方法中使用botProtection参数

在同一函数调用上:

if($instance->validateUser($data, $errors)) {
您需要一个布尔值(true或false),但该方法返回一个数组。如果您想按当前设计的方式使用代码,那么必须像这样使用它

if(!empty($instance->validateUser($data, $errors)) {
在这里,我不太确定是否有必要使用exception。这样设计代码不是更容易吗

if(!empty($instance->validateUser($data, $errors)) {

        $message = 'Validation error';   
}
在验证函数中,“isEmpty”函数是否也在验证客户端是否提供了密码

如果是这种情况,您可以这样验证:

class ARegister {

private $error = 0;

public function register($data) {

    if (!$this->validateUser($data)){

        $this->error++;
    }
}

public function getErrorCount(){
    return $this->error;
}

public resetErrorCount(){
    $this->error = 0;
}
if(empty($errors)) {
        //first validation
}
public function register(array $data) {
public function validateUser(array $data, $botProtection = true) {
if(!in_array($user['password']) or empty($user['password']))
通过这些更正,您的代码应该可以正常工作

以下是我如何设计您的代码的示例(考虑到提供的代码示例):

对于下一部分,如果代码直接在视图中执行,并且您是初学者,请创建一个过程外部控制器文件(所有函数都是公共的…)。如果您是专业人士,则必须创建一个类来封装治疗

您不能直接在视图中进行治疗。视图是一个哑占位符,用于数据表示和收集客户机的输入。它必须执行的唯一操作是显示控制器发送的数据,并将客户端的输入发送回控制器

数据处理是控制器的责任

if (!empty($action) ) {

         $errors =array();

         switch ( $action ) {

            case 'process_payment':

                $instance = new ARegister();

                if($instance->validateUser($data, $errors)) {

                    //the user is valid, do the treatment
                }
                else

                    PageManager::dispayError($errors);
                }

                unset($instance);
         }
    }
下面是一个如何集中显示错误的示例

/**
     * Can be more complexe than that, but I'm at my father's home at four hundred kms away from Montreal right now..
     */
     public static function dispayError($errors, $size = 4){

         if (is_numeric($size)){

             if ($size < 0){
                 $size = 1;
             }
             elseif($size > 5){
                 $size = 5;
             } 
         }
         else{
             $size = 4;
         }

         if (is_scalar($errors)){

             echo '<h' . $size . 'class="ERROR_MESSAGE">' . $errors . '</h' . $size . '><br>';
         }
         elseif (is_array($errors)){

            foreach ($errors as $error){

                if (is_scalar($error)){

                    echo '<h' . $size . 'class="ERROR_MESSAGE">' . $error . '</h' . $size . '><br>';
                }
            }
         }
     }
我花了两个小时写的。我希望您现在有足够的材料来构建高效、可重用且同样重要的安全代码设计

好的成功


来自蒙特利尔的Jonathan Parent-Lévesque

请显示更多代码,您的代码结构目前尚不清楚。这两个PHP文件相关吗?或者是两个不同的页面?我们真的需要更多的代码才能分辨。在哪里调用寄存器函数?你能发布othervalidation.php的完整代码吗?我试图编辑这篇文章以使其更容易理解。我仔细研究了这个想法,我同意它。我有一个公共函数ValidateUser,但我仍然缺少一些东西。我认为问题在于调用方法。有什么想法吗?