How to realize dynamic attribute assignment in yii2

1. The client passes a value to the server such as: username=admin
instantiate a class on the server, and the simple assignment is as follows:
$userform=new UserForm ();
$userform- > username= "admin";
but if the client passes password= "123456"
in the server $userform- > username has to become $userform- > password,
here to consult the gods, is there any good way to achieve the $userform- > name attribute can be dynamically changed, ask for the relevant code examples, thank you!

Mar.06,2021

you can do this and assign values dynamically

foreach($_POST as $key=>$value){
    $userform->{$key} = $value
}

I don't understand why this happens. Can you tell me more about your process? What project are you going to do?

generally speaking, passwords must be encrypted first, because unencrypted passwords have a big problem with commonality, that is, there are more than 2 faces with the same probability. It is not safe to simply compare passwords.

second, if you are going to do the login program, this change will make your safety factor hit bottom in a straight line. If you want to improve the user experience, you can use individual login, phone, third party, email.


$userform- > load ()


the model has its own load method. The first parameter of load is the post array. If the model form is not used, the second parameter sets an empty string and leaves it blank.

Menu