PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
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.
Last edited by ed209 on Mon Feb 06, 2006 9:40 am, edited 1 time in total.
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.
<?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!
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])....