make variable available in class [solved]

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!

Moderator: General Moderators

Post Reply
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

make variable available in class [solved]

Post 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.
Last edited by ed209 on Mon Feb 06, 2006 9:40 am, edited 1 time in total.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

It worked fine for me (after changing "if($param == something){" to ""if($param){").
Last edited by Christopher on Sun Feb 05, 2006 4:46 pm, edited 1 time in total.
(#10850)
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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 :P
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Post 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.
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Post 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.
User avatar
ed209
Forum Contributor
Posts: 153
Joined: Thu May 12, 2005 5:06 am
Location: UK

Solved

Post by ed209 »

this problem can be solved by add '&' infront of $this

Code: Select all

call_user_func(array(&$this, $validate), $otherParams)
Post Reply