2017-03-02 22 views
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);*/ 


    } 


?> 

回答

1

嘗試更新您的getter和setter方法,像這樣:

public function __set($name, $value) { 
    $this->data[$name] = $value; 
} 

public function __get($name) { 
    if (array_key_exists($name, $this->data)) { 
     return $this->data[$name]; 
    } 
    return null; 
} 

http://php.net/manual/en/language.oop5.overloading.php#object.get

其實嘗試了這一點..

class User { 

    private $data = array(); 

    public function __construct($data) { 
     $this->data = $data; 
    } 

    public function __set($name, $value) { 
     $this->data[$name] = $value; 
    } 

    public function __get($name) { 
     if (array_key_exists($name, $this->data)) { 
      return $this->data[$name]; 
     } 
     return null; 
    } 

} 

$u = new User($_POST); 
echo $u->fName; 

您必須確保將要傳遞給User類的所有數據添加到$ data數組中。

您的User類應該可能傳遞$ id,而不是實際數據。 。使用$ id拉取數據..更類似於:

class User { 

    private $data = array(); 

    public function __construct($id = null) { 
     if (!is_null($id) && $id > 0) { 
      $this->id = $id; 
      $this->data = array(); # query user data.. 
     } else { 
      $this->id = 0; 
     } 
    } 

    public function __set($name, $value) { 
     $this->data[$name] = $value; 
    } 

    public function __get($name) { 
     if (array_key_exists($name, $this->data)) { 
      return $this->data[$name]; 
     } 
     return null; 
    } 

    public function save() { 
     if ($this->id > 0) { 
      # UPDATE user data.. 
     } else { 
      # INSERT user data.. 
     } 
    } 

} 

# add new user (no ID provided).. 
$u = new User(); 
$u->fName = $_POST['fName']; 
$u->lName = $_POST['lName']; 
$u->save(); 

# get/update user data (ID provided).. 
$u = new User(1); 
echo $u->fName; # get 
$u->fName = 'NewName'; # update 
$u->save(); 
+0

謝謝你的幫助。 –

+0

你是最受歡迎的! – Rick