babazumbula Posted May 3, 2010 Share Posted May 3, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/200552-access-to-variable-in-constructor-function/ Share on other sites More sharing options...
Ken2k7 Posted May 3, 2010 Share Posted May 3, 2010 How can your function error() return false? It returns a string. As for the if statement, are you sure the "some condition" is true? Quote Link to comment https://forums.phpfreaks.com/topic/200552-access-to-variable-in-constructor-function/#findComment-1052348 Share on other sites More sharing options...
ChemicalBliss Posted May 3, 2010 Share Posted May 3, 2010 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- Quote Link to comment https://forums.phpfreaks.com/topic/200552-access-to-variable-in-constructor-function/#findComment-1052349 Share on other sites More sharing options...
babazumbula Posted May 3, 2010 Author Share Posted May 3, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200552-access-to-variable-in-constructor-function/#findComment-1052355 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2010 Share Posted May 3, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/200552-access-to-variable-in-constructor-function/#findComment-1052357 Share on other sites More sharing options...
babazumbula Posted May 3, 2010 Author Share Posted May 3, 2010 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' Quote Link to comment https://forums.phpfreaks.com/topic/200552-access-to-variable-in-constructor-function/#findComment-1052372 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2010 Share Posted May 3, 2010 I think you'll need to post up more code. Quote Link to comment https://forums.phpfreaks.com/topic/200552-access-to-variable-in-constructor-function/#findComment-1052376 Share on other sites More sharing options...
babazumbula Posted May 3, 2010 Author Share Posted May 3, 2010 Hmm... It would be a help for me if I now this is a correct logic. In essence, should this chunk of code work properly? I mean if this is the correct way to alter $this->var_err then obviously mistake is somewhere else. Quote Link to comment https://forums.phpfreaks.com/topic/200552-access-to-variable-in-constructor-function/#findComment-1052383 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2010 Share Posted May 3, 2010 Well, in that die statement, you can have it print out the value of $this->var_err. That would eliminate any doubts whether or not that statement works. Quote Link to comment https://forums.phpfreaks.com/topic/200552-access-to-variable-in-constructor-function/#findComment-1052384 Share on other sites More sharing options...
babazumbula Posted May 3, 2010 Author Share Posted May 3, 2010 Just did it and it echo out: 1 So it works! Quote Link to comment https://forums.phpfreaks.com/topic/200552-access-to-variable-in-constructor-function/#findComment-1052387 Share on other sites More sharing options...
babazumbula Posted May 3, 2010 Author Share Posted May 3, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/200552-access-to-variable-in-constructor-function/#findComment-1052392 Share on other sites More sharing options...
Ken2k7 Posted May 3, 2010 Share Posted May 3, 2010 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? 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. Quote Link to comment https://forums.phpfreaks.com/topic/200552-access-to-variable-in-constructor-function/#findComment-1052395 Share on other sites More sharing options...
PFMaBiSmAd Posted May 3, 2010 Share Posted May 3, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200552-access-to-variable-in-constructor-function/#findComment-1052398 Share on other sites More sharing options...
babazumbula Posted May 3, 2010 Author Share Posted May 3, 2010 Sorry for delay of crucial information . I prefer not to employ session var,instead I defined static $var_err; but still won't work Quote Link to comment https://forums.phpfreaks.com/topic/200552-access-to-variable-in-constructor-function/#findComment-1052401 Share on other sites More sharing options...
PFMaBiSmAd Posted May 3, 2010 Share Posted May 3, 2010 static just means a variable is available between separate calls within one instance of a page. Quote Link to comment https://forums.phpfreaks.com/topic/200552-access-to-variable-in-constructor-function/#findComment-1052403 Share on other sites More sharing options...
babazumbula Posted May 3, 2010 Author Share Posted May 3, 2010 One thing you can do is make it static. Not sure that I understand completely. Quote Link to comment https://forums.phpfreaks.com/topic/200552-access-to-variable-in-constructor-function/#findComment-1052404 Share on other sites More sharing options...
babazumbula Posted May 3, 2010 Author Share Posted May 3, 2010 OK, Thanks a lot guys ! I'll be employing session var since "static" is not something I completely understand at the moment. Quote Link to comment https://forums.phpfreaks.com/topic/200552-access-to-variable-in-constructor-function/#findComment-1052410 Share on other sites More sharing options...
Zane Posted May 3, 2010 Share Posted May 3, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/200552-access-to-variable-in-constructor-function/#findComment-1052413 Share on other sites More sharing options...
babazumbula Posted May 3, 2010 Author Share Posted May 3, 2010 why don't you make var_err a private var. private var_err; // Then later on $this->var_err = true; . . Doesn't work for me. Quote Link to comment https://forums.phpfreaks.com/topic/200552-access-to-variable-in-constructor-function/#findComment-1052427 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.