Page 1 of 1
make variable available in class [solved]
Posted: Sun Feb 05, 2006 4:35 pm
by ed209
Class
Code: Select all
class myClass{
var $message ="";
function validator($param){
if($param == something){
return true;
}else{
$this->message = "parameter not ok";
return false;
}
}
function checkPostedData($data){
if($this->validator($data)){
echo "That parameter is ok";
}else{
echo $this->message;
}
}
}
Why doesn't the '$this->message' get made available to the checkPostedData ? Is it because I returned false on the 'validator' function. I thought that $this->someVar was available to everything in the class?
Thanks for your help.
Posted: Sun Feb 05, 2006 4:46 pm
by Christopher
It worked fine for me (after changing "if($param == something){" to ""if($param){").
Posted: Sun Feb 05, 2006 4:46 pm
by Chris Corbyn
$this->message is available to everything in there. You'd be seeing errors if not
Have you considered that your logic could be wrong?
EDIT | SCRAP MY LAST EDIT... I'M AN IDIOT

Posted: Mon Feb 06, 2006 4:37 am
by ed209
I can't figure this one out. The logic is ok, tested that. Something is happenning with $this->_error_msg. Maybe if I put some of the actual code it may help.
Code: Select all
<?php
class myFormClass{
// in this case $validate = '_validate_email'
function makeFormElement($someParameters, $validate){ // summarised
if( isset( $_POST[$this->form_id] ) ){ // the form is submitted to itself so this checks for submission
$this->check_posted_data($validate, $name);
}
return "<input ... etc".$this->_error_msg;
}
function check_posted_data($validate, $name_Of_Form_Input_Element){
if(call_user_func(array($this, $validate), $_POST[$name_Of_Form_Input_Element])){
$this->_error_msg = "";
return true;
}else{
$this->_error = TRUE;
return false;
}
}
function _validate_email($intext){
$theresults = ereg("^[^@ ]+@[^@ ]+\.[^@ \.]+$", $intext, $trashed);
if ($theresults){
return true;
}else{
$this->_error_msg = "<span class=\"warning\">This email address apears to be invalid</span>";
return false;
}
}
}
?>
For some reason $this-_error_msg isn't making it through. It gets set ok as I can echo it out from _validate_email function. But it doesn't seem to make it to check_posted_data and ultimately makeFormElement.
I've tried to simplify the problem as much as I can. Maybe the error lies elsewhere, but posting 500 lines of code may not be very useful!
Thanks for the help.
Posted: Mon Feb 06, 2006 9:10 am
by ed209
I have discovered that the problem lies with
Code: Select all
if(call_user_func(array($this, $validate), $_POST[$name])){
because if I substitue it with calling the function directly it works (like this):
Code: Select all
if($this->_validate_email($_POST[$name])){
So when I try and set $this->_error_msg the '$this' part must mean something else (i.e. another instance?) when using call_user_func(array($this, $validate), $_POST[$name])....
Other variations tried unsuccessfully:
Code: Select all
if(call_user_func($validate, $_POST[$name])){
// Where $validate = '$this->_validate_email'
// And $validate = '$classInstanceName->_validate_email';
// And $validate = '_validate_email';
// And $validate =array('ClassName', '_validate_email')
any ideas ?
Thanks,
Ed.
Solved
Posted: Mon Feb 06, 2006 9:40 am
by ed209
this problem can be solved by add '&' infront of $this
Code: Select all
call_user_func(array(&$this, $validate), $otherParams)