dongshao1021 2013-03-03 13:50
浏览 6
已采纳

类级变量的奇怪行为

I've looked at this code for over an hour, and it looks fine to me. However, the description variable is losing scope when I use the insertSubmit() function. The variable is set during construction but not when I use the insertSubmit(). Am I missing something here?

<?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条回答 默认 最新

  • dongmu3187 2013-03-03 14:01
    关注

    The problem is located at errorBlank() function, when you do this:

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

    Instead of doing this:

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

    Note the extra equal sign! You were assigning null to $this->description, instead of comparing them!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助