1
我尝试从另一个类创建对象并从第二个类访问它。对象被创建,当我使用第一个类打印对象时,它将会打印。但是,当我使用第二类并调用getter方法并尝试打印时,它会给出错误。使用__get时未定义的变量:php中的user_created_date方法
当我尝试在第二个类中创建第一个类的对象并尝试调用getter方法时,它将给出未定义变量$any_varible
的错误。
头等舱:
<?php
namespace pages;
class User {
private static $first_name;
private static $last_name;
private static $nic;
private static $email;
private static $password;
private static $tel;
private static $address;
private static $city;
private static $postal_code;
private static $dob;
private static $food;
private static $pet;
private static $smoke;
private static $chat;
private static $music;
private static $gender;
private static $user_created_date;
public function __construct($first_name,$last_name,$nic,$email,$password,$tel,$address,$city,$postal_code,$dob,$food,$pet,$smoke,$chat,$music,$gender,$user_created_date) {
$this->$first_name=$first_name;
$this->$last_name=$last_name;
$this->$nic=$nic;
$this->$email=$email;
$this->$password=$password;
$this->$tel=$tel;
$this->$address=$address;
$this->$city=$city;
$this->$postal_code=$postal_code;
$this->$dob=$dob;
$this->$food=$food;
$this->$pet=$pet;
$this->$smoke=$smoke;
$this->$chat=$chat;
$this->$music=$music;
$this->$gender=$gender;
$this->$user_created_date=$user_created_date;
}
public function __get($property) {
if (property_exists($this, $property)) {
return $this->$property;
}
}
public function __set($property, $value) {
if (property_exists($this, $property)) {
$this->$property = $value;
}
}
} ?>
<?php
namespace pages;
include '../Model/User.php';
include 'Database.php';
{
$cars = array(0,0,0,0,0);
$availableChoice=$_POST['choice'];
$today=date('Y/m/d');
$count=0;
foreach ($availableChoice as $name){
$cars[$count]=1;
$count++;
}
$usernew=new User($_POST['fName'],$_POST['lName'],$_POST['nic'],$_POST['email'],$_POST['password'],$_POST['mobile'],$_POST['address'],$_POST['city'],$_POST['postalCode'],$_POST['dob'],$cars[0],$cars[1],$cars[2],$cars[3],$cars[4],$_POST['gender'],$today);
//echo $_POST['email'];
//echo $user->$email;
echo $usernew->__get($user_created_date);
/*$newDatabase=new Database();
$connection=$newDatabase->CreateConnection();
$newDatabase->InsertUser($user,$connection);*/
}
?>
谢谢你的帮助。 –
你是最受欢迎的! – Rick