Php 将变量从一个类传递到另一个类
您好,我正在创建一个或多个注册类,用户填写一个表单。第一个类创建一个用户记录,然后它移动到下一个类,将其所有详细信息放入用户表中。然而,我很难将一个变量从第一个类传递到另一个类。 第一个类中的$user_id也需要添加到第二个类的末尾。有什么办法吗Php 将变量从一个类传递到另一个类,php,class,variables,Php,Class,Variables,您好,我正在创建一个或多个注册类,用户填写一个表单。第一个类创建一个用户记录,然后它移动到下一个类,将其所有详细信息放入用户表中。然而,我很难将一个变量从第一个类传递到另一个类。 第一个类中的$user_id也需要添加到第二个类的末尾。有什么办法吗 include('../../../Connections/pdo.php'); /*reg class*/ class newreg { private $db; public function __construct(){ $thi
include('../../../Connections/pdo.php');
/*reg class*/
class newreg {
private $db;
public function __construct(){
$this->db = new connection();
$this->db = $this->db->dbconnect();
}
public function addusr($username,$password,
$usrTypeId,$usrFstRegDate,
$usrStatus, $activated,
$chgPwd,$fstActivated,
$lstlogin,$registered ){
if(!empty($username)){
$st = $this->db->prepare(" INSERT INTO users
(user_name, user_password,
user_type_id,user_first_registration_date,
user_status, activated,
change_password, first_activated,
last_log_in, registered) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
$st->bindParam(1, $username);
$st->bindParam(2, $password);
$st->bindParam(3, $usrTypeId);
$st->bindParam(4,$usrFstRegDate);
$st->bindParam(5, $user_status);
$st->bindParam(6, $activated);
$st->bindParam(7, $chgPwd);
$st->bindParam(8, $fstActivated);
$st->bindParam(9, $lstlogin);
$st->bindParam(10, $registered);
$st->execute();
echo 'Inserted: ';
return $this->db->lastInsertId();
} else {
echo "Fail";
}
}
}
class individReg {
private $db;
public function __construct(){
$this->db = new connection();
$this->db = $this->db->dbconnect();
}
public function addIndivid($title,$firstname,$lastname,$knownAs,$gender,$dob,$nsn,$email,
$addy1,$addy2,$addy3,$city,$region,$postcode,$pnumber,
$wnumber,$vquestion,$vanswer,$usr_ind_id,$user_type,$user_id){
if(!empty($title)){
$st = $this->db->prepare(" INSERT INTO users_individual (
user_individual_title, user_individual_first_name, user_individual_last_name,
user_individual_known_as,user_gender, user_individual_dob,
nsn_id, user_individual_email,user_individual_address_line_1,user_individual_address_line_2,
user_individual_address_line_3,user_individual_city, region, user_individual_postcode,
user_individual_phone_home, user_individual_phone_work, user_individual_verification_question,
user_individual_verification_answer,user_individual_id,user_type_id,user_id ) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
$st->bindParam(1, $title);
$st->bindParam(2, $firstname);
$st->bindParam(3, $lastname);
$st->bindParam(4, $knownAs);
$st->bindParam(5, $gender);
$st->bindParam(6, $dob);
$st->bindParam(7, $nsn);
$st->bindParam(8, $email);
$st->bindParam(9, $addy1);
$st->bindParam(10, $addy1);
$st->bindParam(11, $addy1);
$st->bindParam(12, $city);
$st->bindParam(13, $region);
$st->bindParam(14, $postcode);
$st->bindParam(15, $pnumber);
$st->bindParam(16, $wnumber);
$st->bindParam(17, $vquestion);
$st->bindParam(18, $vanswer);
$st->bindParam(19, $usr_ind_id);
$st->bindParam(20, $user_type);
$st->bindParam(21, $user_id);
$st->execute();
echo "Success!";
} else {
echo "Fail";
}
}
}
-----------------这是我调用类的页面--------
只需使用newreg::addusr()函数返回最后一个\u insert\u id
在您的情况下,它将是:
return $this->db->lastInsertId();
将此代码添加到newreg::addusr函数的末尾,然后将此id传递给individReg::addIndivid()您的意思是将
$user\u id
传递给$usr\u ind\u id
对不起,我忘了将$usr\u id添加到第二个类的末尾。如何使用这两个类?这个问题如何?解决了吗?这就是我一直在尝试的方法不知道为什么它不起作用。谢谢,但我会再试一次。请求这些函数的代码可能如下所示:$user\u id=newreg::addusr($var1,$var2…);individReg::addIndivid($user\u id,$var1,…)代码>如何传递返回值$this->db->lastInsertId();进入下一个classIve更新了代码。用户id是自动识别的,不应该是第一流的。现在我把return$this->db->lastInsertId()放进去;在第一节课结束时,我只需要帮助将这些信息输入到第二节课。
return $this->db->lastInsertId();