开发者

PHP: Any failure? How to make `echo $report` return right value trough `class validate`?

Why echo $report has empty value? Any failure in class validate?

How to make it return right value trough class validate?

<?php
class validate{
    function status($status){
        header('Location:',true,$status);
        $report="Error: $status";
    }
    function fine(){
        $report='Ok!';
    }
}
$validate=new validate($id);
//$id=0;
$id=(isset($id) ? $id : NULL);
(($id==0) ? $validate->fine() 开发者_如何学JAVA: $validate->status('404'));
echo $report; // Why it has empty value? How to solve it?
?>


Declare it as a public variable in the class, set it using $this->report, and echo it using $validate->report.

Example:

class validate{
    public $report;
    function status($status){
        header('Location:',true,$status);
        $this->report="Error: $status";
    }
    function fine(){
        $this->report='Ok!';
    }
}


$report is a class variable in your validate object. echo $report is looking for a variable in the local scope, not your object. Try echo $validate->report instead.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜