类级变量的奇怪行为

时间:2013-03-03 13:50:07

标签: php

我看了这个代码超过一个小时,对我来说看起来很好。但是,当我使用insertSubmit()函数时,描述变量正在失去范围。变量在构造期间设置,但在我使用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;
}   
}

1 个答案:

答案 0 :(得分:2)

当您执行此操作时,问题位于errorBlank()函数:

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

而不是这样做:

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

注意额外的等号!您将null分配给$this->description,而不是将它们进行比较!