参数值未在 PHP 类 中传递

Argument value is not being passed in PHP classes

我有一个场景,我想使用面向对象 PHP8 创建一个用户,我有我的 users.php 页面,其中有表单,这个问题似乎是我不认为的来自我的对象表单($objForm)的方法被正确地传递到我的对象验证($objValidation):

users.php

<?php

use A\B\App\Session;
use A\B\App\Login;
use A\B\App\User;
use A\B\App\Form;
use A\B\App\Validation;

require_once("../../vendor/autoload.php");

$session = new Session();
if(!$session->isLogged()) {
    Login::redirectTo("login");
}



$objForm = new Form();
// passing in the object to validation
$objValidation = new Validation($objForm);
$objUser = new User();

if($objForm->isPost("name")) {

    $objValidation->expected = ["name"];
    $objValidation->required = ["name"];

    $objValidation->special = ["email" => "email"];
    $objValidation->postFormat = ["password" => "password"];

    $email = $objForm->getPost("email");
    $existingUser = $objUser->getUserByEmail($email);
    if($existingUser) {
        $objValidation->addToErrors("user_exists");
    } 

    if($objValidation->isValid()) {
        echo "good";
    } else {
        echo "bad";
    }



}
 
?>

<form action="" method="post">
    
    <p>
        <input type="text" name="name" placeholder="User name" required="">
    </p>
    <?php echo $objValidation->validate('user_exists'); ?>
    <p>
        <input type="email" name="email" placeholder="User email" required="">
    </p>
    <p>
        <input type="password" name="password" placeholder="Password" required="">
    </p>
    <p>
        <input type="submit" name="" value="Add user">
    </p>

</form>

我只是想回答是或否,看看它是否通过了这一点,所以我实际上并没有调用方法来创建用户,因为代码之前失败了。

下面是我的 2 classes:

Form.php

<?php

declare(strict_types = 1);

namespace A\B\App;

class Form
{

    public function isPost(string $field): bool
    {

        if(!empty($field)) {
            if(isset($_POST[$field])) {
                return true;
            }
            return false;
        } else {
            if(!empty($_POST)) {
                return true;
            }
            return false;
        }

    }

    public function getPost(string $field)
    {

        if(!empty($field)) {
            return $this->isPost($field) ? strip_tags($_POST[$field]) : null;
        }

    }

    public function getPostArray(array $field): array
    {

        $out = [];
        if($this->isPost()) {
            foreach($_POST as $key => $value) {
                if(!empty($field)) {
                    if(in_array($key, $expected)) {
                        $out[$key] = strip_tags($value);
                    }
                } else {
                    $out[$key] = strip_tags($value);
                }
            }
        }
        return $out;

    }


}

这里是验证 class:

Validation.php

<?php

declare(strict_types = 1);

namespace A\B\App;

class Validation 
{


    private array $error = [];

    public array $message = ["user_exists" => "This user exists in the Db"];

    public array $expected = [];

    public array $required = [];

    public array $post = [];

    private $objForm;

    public array $test = []; 

    public array $postRemove = [];

    public array $postFormat = [];

    public array $special = []; 

    public function __construct(Form $form)
    {

        $this->objForm = $form;

    }

    public function process()
    {

        if($this->objForm->isPost() && !empty($this->required)) {

            $this->post = $this->objForm->getPostArray($this->expected);
            if(!empty($this->post)) {
                foreach($this->post as $key => $value) {
                    $this->check($key, $value);
                }
            }

        }

    }

    public function check($key, $value)
    {

        if(!empty($this->special) && array_key_exists($key, $this->special)) {
            $this->checkSpecial($key, $value);
        } else {
            if(in_array($key, $this->required) && empty($value)) {
                $this->addToErrors($key);
            }
        }

    }

    public function checkSpecial($key, $value)
    {

        switch($this->special[$key]) {
            case 'email':
            if(!$this->isEmail($value)) {
                $this->addToErrors($key);
            }
            break;
        }

    }

    public function isEmail($email)
    {

        if(!empty($email)) {
            $result = filter_var($email, FILTER_VALIDATE_EMAIL);
            return !$result ? false : true;
        }
        return false;

    }

    public function isValid()
    {

        $this->process();
        if(empty($this->errors) && !empty($this->post)) {
            // remove unwanted fields
            if(!empty($this->postRemove)) {
                foreach($this->postRemove as $value) {
                    unset($this->post[$value]);
                }
            }
            // format required fields
            if(!empty($this->postFormat)) {
                foreach($this->postFormat as $key => $value) {
                    $this->format($key, $value);
                }
            }
            return true;
        }
        return false;

    }

    public function format($key, $value)
    {

        switch($value) {
            case 'password':
            $this->post[$key] = Login::passwordEncrypt($this->post[$key]);
            break;
        }

    }


    public function addToErrors($key)
    {

        $this->errors[] = $key;

    }

    public function validate($key)
    {

        if(!empty($this->errors) && in_array($key, $this->errors)) {
            return $this->wrapWarn($this->message[$key]);
        }

    }

    public function wrapWarn($mess)
    {

        if(!empty($mess)) {
            return "<span class=\"warn\" >{$mess}</span>";
        }

    }

}

我已经测试了 validations.php 和 form.php 的其他虚拟函数,它们都有效,所以问题肯定出在与这两个 class 的通信之间。处理方法无法识别或无法从 $this->objForm->isPost() 调用中获取值。这是我收到的错误消息,它告诉我没有传入任何值:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function A\B\App\Form::isPost(), 0 passed in /Applications/MAMP/htdocs/project/src/App/Validation.php on line 41 and exactly 1 expected in /Applications/MAMP/htdocs/project/src/App/Form.php:10 Stack trace: #0 /Applications/MAMP/htdocs/project/src/App/Validation.php(41): A\B\App\Form->isPost() #1 /Applications/MAMP/htdocs/project/src/App/Validation.php(94): A\B\App\Validation->process() #2 /Applications/MAMP/htdocs/project/src/Pages/users.php(36): A\B\App\Validation->isValid() #3 {main} thrown in /Applications/MAMP/htdocs/project/src/App/Form.php on line 10

注意:由于被卡住,我还没有将类型 declarations/hints 应用于所有内容。

为了不改变太多的代码库(改变方法的所有引用),你应该这样改变isPost()函数的签名:

public function isPost(string $field = ""): bool