Php 在我的类之外定义声明的变量

Php 在我的类之外定义声明的变量,php,class,Php,Class,我有一个类,在这个类中,我首先执行SQL检查以查看是否存在某些特定数据,如果不存在,则插入一条新记录 我想以某种方式定义一个变量($feedback),指示我插入了什么或没有插入什么-我想在我的主PHP文件(我包含了这个类)的回调中使用这个变量 我的班级 class registerVote { function registerVote(){ } function init() { //Define some vars return true; } function

我有一个类,在这个类中,我首先执行SQL检查以查看是否存在某些特定数据,如果不存在,则插入一条新记录

我想以某种方式定义一个变量($feedback),指示我插入了什么或没有插入什么-我想在我的主PHP文件(我包含了这个类)的回调中使用这个变量

我的班级

class registerVote {

function registerVote(){

}

function init() {
    //Define some vars  
    return true;
}

function save() {
        //Sql count query
        $res = mysql_query($sql) or die( mysql_error());
        $count = mysql_result($res, 0); 

        //If no records are found then INSERT
        if($count < 1){
            //Some INSERT query
            $feedback = "new";
        } else {
            $feedback = "old";
        }

    if($res) {
        return $feedback;
    }
}

}

上述内容自然不起作用-因此我自然想知道我是否能够实现我试图说明的内容?

您可以让您的
registerNote::save()
函数返回
$feedback
。这似乎是一种合理的做法


次要:如果
$res
false
将触发
或die
案例,脚本将退出,则不需要
返回
周围的
if

您可以让
registerNote::save()
函数返回
$feedback
。这似乎是一种合理的做法


次要:如果
$res
false
将触发
或die
案例,脚本将退出,则不需要
返回
周围的
if

只需捕获返回值:

include_once ('../class/class_vote.php');
$registerVote = new registerVote();

if($registerVote->init()) {
    $feedback = $registerVote->save();
    if($feedback) {
        //Print out my $feedback from the class
        if ($feedback == "new") {
            echo "new";
        } else {
            echo "old";
        }
    }
}

顺便说一下,
save()
应该总是返回一些内容。

只需捕获返回值:

include_once ('../class/class_vote.php');
$registerVote = new registerVote();

if($registerVote->init()) {
    $feedback = $registerVote->save();
    if($feedback) {
        //Print out my $feedback from the class
        if ($feedback == "new") {
            echo "new";
        } else {
            echo "old";
        }
    }
}

顺便说一下,
save()
应该总是返回一些内容。

返回要分配的值

设置对象静态属性的值

class Feedback {
    static public $results;
}
然后你可以打电话

Feedback::$results = "new";

返回要指定的值

设置对象静态属性的值

class Feedback {
    static public $results;
}
然后你可以打电话

Feedback::$results = "new";

您正确返回<代码> $反馈<代码>,但是您应该考虑返回一个<代码>布尔,<代码>真< <代码>或<代码> false <代码>。但是,您需要存储
$registerNote->save()调用的返回,然后才能检查它。您还可以将
init
函数更改为
\uu-construct
,这样您就不必调用它了。你的班级将是这样的:

class registerVote
{

    public function __construct()
    {
        //Define some vars  
        return true;
    }

    public function registerVote()
    {
    }

    public function save()
    {
        //Sql count query
        $res = mysql_query($sql) or die( mysql_error());
        $count = mysql_result($res, 0); 

        if($count === 0)
        {
            // store some stuff.
            return TRUE;
        }
        return FALSE;
   }
}
include_once ('../class/class_vote.php');
$registerVote = new registerVote();

if($registerVote->save())
{
    echo "New!";
}
else
{
    echo "Old!";
}
您的PHP文件如下所示:

class registerVote
{

    public function __construct()
    {
        //Define some vars  
        return true;
    }

    public function registerVote()
    {
    }

    public function save()
    {
        //Sql count query
        $res = mysql_query($sql) or die( mysql_error());
        $count = mysql_result($res, 0); 

        if($count === 0)
        {
            // store some stuff.
            return TRUE;
        }
        return FALSE;
   }
}
include_once ('../class/class_vote.php');
$registerVote = new registerVote();

if($registerVote->save())
{
    echo "New!";
}
else
{
    echo "Old!";
}

您正确返回<代码> $反馈<代码>,但是您应该考虑返回一个<代码>布尔,<代码>真< <代码>或<代码> false <代码>。但是,您需要存储
$registerNote->save()调用的返回,然后才能检查它。您还可以将
init
函数更改为
\uu-construct
,这样您就不必调用它了。你的班级将是这样的:

class registerVote
{

    public function __construct()
    {
        //Define some vars  
        return true;
    }

    public function registerVote()
    {
    }

    public function save()
    {
        //Sql count query
        $res = mysql_query($sql) or die( mysql_error());
        $count = mysql_result($res, 0); 

        if($count === 0)
        {
            // store some stuff.
            return TRUE;
        }
        return FALSE;
   }
}
include_once ('../class/class_vote.php');
$registerVote = new registerVote();

if($registerVote->save())
{
    echo "New!";
}
else
{
    echo "Old!";
}
您的PHP文件如下所示:

class registerVote
{

    public function __construct()
    {
        //Define some vars  
        return true;
    }

    public function registerVote()
    {
    }

    public function save()
    {
        //Sql count query
        $res = mysql_query($sql) or die( mysql_error());
        $count = mysql_result($res, 0); 

        if($count === 0)
        {
            // store some stuff.
            return TRUE;
        }
        return FALSE;
   }
}
include_once ('../class/class_vote.php');
$registerVote = new registerVote();

if($registerVote->save())
{
    echo "New!";
}
else
{
    echo "Old!";
}