Jump to content

Access to variable in constructor function


babazumbula

Recommended Posts

Here's one beginner's question:

I have this code

class Membership_model extends Model{
   
    var $var_err;

    function Membership_model(){
        parent::Model();

        $this->var_err = false;
    }

    function upload_image(){
    ...

    if(some condition){$this->var_err = true;}
    ...
   }

   function error(){

            if($this->var_err){
            $error ='error';
            }
            else{$error = "";}
            return $error;
        }



}

my problem is that function error() always returns false, so line if(some condition){$this->var_err = true;} never gets access to variable $this->var_err = false; I defined in the constructor function.

How can I access to this variable and change it from false to true for certain condition?

Im not 100% on what you want and why but a shot in the wild west:

 

change

    var $var_err;

    function Membership_model(){
        parent::Model();

        $this->var_err = false;
    }

 

to

    var $var_err=false;

    function Membership_model(){
        parent::Model();
    }

 

-cb-

How can your function error() return false? It returns a string.

 

As for the if statement, are you sure the "some condition" is true?

My fault,  function error() does return string, but $this->var_err keeps holding value of 'false'. I'm pretty sure for 'some condition' is true.

 

@ChemicalBliss Tried, no use.

 

If that's the case, before the if statement in your error function, put this:

 

$this->var_err = true;

 

Then if error() returns 'error' then your if conditional in your upload_image function is false. Did you even check if the if statement runs as expected?

I tried this: if(some condition){$this->var_err = true;die();} and it runs expected, If I define $this->var_err = false; then function error()every time returns empty string,however if i define $this->var_err = true; then function error()every time returns 'error'

The problem must be for reason that at the end of function upload_image() I redirect users to another page and then I call function error() which in that time doesn't hold $this->var_err anymore as true? :confused:

Thanks for telling us that now. >_>

 

One thing you can do is make it static. Another thing you can do is store a session var.

I redirect users to another page ... doesn't hold $this->var_err  anymore

You do know that web servers are stateless? They don't know or care what happened on any page request before the current one or on any page request after the current one. All resources used on a page are destroyed at the end of the processing of that page request.

why don't you make var_err a private var.

private var_err;

// Then later on
$this->var_err = true;
.
.
.
//in the error() function
.
.
if($this->var_err) return "error";
else return "";

 

I'm pretty sure var is deprecated too.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.