2013-03-03 94 views
0

我已經看了這個代碼超過一個小時,它看起來對我來說很好。但是,當我使用insertSubmit()函數時,description變量正在丟失範圍。該變量在構造期間設置,但不是在使用insertSubmit()時設置。我在這裏錯過了什麼嗎?奇怪的行爲與類級變量

<?php session_start(); 

class SubmitNew { 

var $title = ""; 
var $category = ""; 
var $summary = ""; 
var $description = ""; 
var $username = ""; 
var $timestamp = ""; 
var $errors = ""; 
var $errorStr = ""; 
var $link = ""; 
var $db = ""; 

public function __construct() { 
    $this->setVariables(); 
    $this->errors = 0; 
    $this->p_id = 1; 

} 
public function setVariables() { 
    $this->title = "1"; 
    $this->category = "2"; 
    $this->summary = "3"; 
    $this->description = "4"; 
    echo $this->description; 
    $this->timestamp = mktime(date("G"), date("i"), 
    date("s"), date("m") , date("d"), date("Y")); 
} 
public function errorBlank() { 
    if($this->title == null) { 
     $this->errorStr = $this->errorStr."blanktitle"; 
     $this->errors++;  
    } if($this->summary == null) { 
     $this->erroStr = $this->errorStr."blanksummary"; 
     $this->errors++;    
    } if($this->description = null) { 
     $this->erroStr = $this->errorStr."blankdescription"; 
     $this->errors++;  
    } 
} 
public function errorAll() { 
    if($this->errors == 0) { 
     return "success"; 
    } else { 
     return $this->errorStr; 
    } 
} 
public function insertSubmit() { 
    echo $this->description; 
} 
} 
+2

請說明你是如何使用這個類的,因爲當我執行以下時,我會看到「4」echo'd兩次:'$ s = new SubmitNew(); $ s-> insertSubmit();' – 2013-03-03 13:55:03

+0

我看到'$ this-> description = null',所以你給'$ this-> description'分配'null'。使用'==' – metadings 2013-03-03 13:58:53

+0

metadings謝謝! – user2128903 2013-03-03 13:59:05

回答

2

問題是位於errorBlank()功能,當你這樣做:

} if($this->description = null) { 

而不是做這個的:

} if($this->description == null) { 

注意額外的等號!您將null分配到$this->description,而不是將它們進行比較!

+1

是的,這是問題所在。謝謝。它總是小的那些讓我:)。 – user2128903 2013-03-03 14:06:15