user1231561
user1231561

Reputation: 3359

Defining declared variable outside of my class

I have a class where I first do a SQL check to see if some certain data exist and if not then I INSERT a new record.

I'd like to somehow define a variable ($feedback) indicating ive either inserted something or havent - I want to use this var in a callback in my main PHP file (where ive included the class)

My class

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;
    }
}

}

My PHP FILE:

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

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

The above naturally doesnt work - so im naturally wondering if im able to achieve what ive tried to illustrate?

Upvotes: 0

Views: 51

Answers (4)

Jordan Doyle
Jordan Doyle

Reputation: 3026

You're correctly returning $feedback but you should consider returning a boolean, true or false for example. You need to store the return of the $registerVote->save() call, though, and then you can check it. You could also change your init function to __construct, that way you don't have to call it. Your class will be like this:

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;
   }
}

Your PHP file would be like this:

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

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

Upvotes: 0

Orangepill
Orangepill

Reputation: 24645

return the value to be assigned.

Or

Set the value on a static property of an object.

class Feedback {
    static public $results;
}

then you can call

Feedback::$results = "new";

Upvotes: 0

Vedran Šego
Vedran Šego

Reputation: 3765

Just capture the return value:

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";
        }
    }
}

By the way, save() should always return something.

Upvotes: 2

Halcyon
Halcyon

Reputation: 57709

You can have your registerVote::save() function return the $feedback. That seems like a reasonable way of doing it.

Minor: if $res is false the or die case will have triggered and your script will exit, the if around the return isn't needed.

Upvotes: 1

Related Questions