Jump to content

how to access class member ?


arif_shariati

Recommended Posts

I am having problem with calling class member from with the function in that class.

<?php
class board
{


function __construct () 
{
	$num_sheep_init=20;
 	$num_lion_init=2;
	$board_value=array(0=>'',
								  1=>'',
								  2=>'',
								  3=>'',
								  4=>'',
								  5=>'',
								  6=>'',
								  7=>'',
								  8=>'',
								  9=>'',
								  10=>'',
								  11=>'',
								  12=>'',
								  13=>'',
								  14=>'',
								  15=>'',
								  16=>'',
								  17=>'',
								  18=>'',
								  19=>'',
								  20=>'',
								  21=>'',
								  22=>'',
								  23=>'',
								  24=>'');
	$random_array_value=array (0,1,2,3,4,5,7,9,10,11,13,14,15,17,19,20,21,22,23,24);
	$selected_index=array();
	$randd=array();
	$countt=0;
	}
function init_board ()
{
	$this->board_value['6']="S";

 

Above is part of the code where i am having problem. It says that it can not access empty property //$this->board_value['6']="S";

Link to comment
https://forums.phpfreaks.com/topic/222610-how-to-access-class-member/
Share on other sites

right on... your board_value variable dies when __construct is finished.. what you should do is something like:

<?php
class board
{
  protected $board_value;

  public function __construct(){
    $this->board_value = array(***)
  }
}

that way you can access it by: $this->board_value[index].

 

hope you get the picture  :)

Thanks it did work. But how can i use form values in class functions ??

<?php
class board
{
	public $num_sheep_init=20;
 	public $num_lion_init=2;
	public $board_value=array(0=>'',
								  1=>'',
								  2=>'',
								  3=>'',
								  4=>'',
								  5=>'',
								  6=>'',
								  7=>'',
								  8=>'',
								  9=>'',
								  10=>'',
								  11=>'',
								  12=>'',
								  13=>'',
								  14=>'',
								  15=>'',
								  16=>'',
								  17=>'',
								  18=>'',
								  19=>'',
								  20=>'',
								  21=>'',
								  22=>'',
								  23=>'',
								  24=>'');
	public $random_array_value=array (0,1,2,3,4,5,7,9,10,11,13,14,15,17,19,20,21,22,23,24);
	public $selected_index=array();
	public $randd=array();
	public $countt=0;

function __construct () 
{

	}
function init_board ()
{
	$this->board_value['6']="S";
	$this->board_value['8']="S";
	$this->board_value['16']="S";
	$this->board_value['18']="S";

	while ($this->countt!=2)
	{
		$v=array_rand($this->random_array_value);
		array_push ($this->randd,$v);//find random value from the array
		array_push($this->selected_index,$v); // push the random vlue in the selected array
		//echo "now pushed ".$v."<br>";
		$this->random_array_value=array_diff($this->random_array_value,$this->randd);// delete the random selected array from the array		
		$this->countt++;
	}
	for ($k=0;$k<sizeof($this->selected_index);$k++)
	{
		$this->board_value[$this->selected_index[$k]]="L"; //insert the positions of lions
	}
	}
function show_board ()
{
		echo "<table border='1' bordercolor='#CCCCCC' id='board'>";
	echo "<tr>";
		echo "<td id='td0'><center>".$this->board_value['0']."</center></td>";
		echo "<td id='td1'><center> ".$this->board_value['1']."</center></td>";
		echo "<td id='td2'><center> ".$this->board_value['2']."</center></td>";
		echo "<td id='td3'><center>".$this->board_value['3']."</center></td>";
		echo "<td id='td4'><center>".$this->board_value['4']."</center></td>";
	echo "</tr>";

	echo "<tr>";
		echo "<td id='td5'><center>".$this->board_value['5']."</center></td>";
		echo "<td id='td6'><center>".$this->board_value['6']."</center></td>";
		echo "<td id='td7'><center> ".$this->board_value['7']."</center></td>";
		echo "<td id='td8'> <center>".$this->board_value['8']."</center></td>";
		echo "<td id='td9'> <center>".$this->board_value['9']."</center></td>";
	echo "</tr>";

	echo "<tr>";
		echo "<td id='td10'> <center>".$this->board_value['10']."</center></td>";
		echo "<td id='td11'> <center>".$this->board_value['11']."</center></td>";
		echo "<td id='td12'> <center>".$this->board_value['12']."</center></td>";
		echo "<td id='td13'><center> ".$this->board_value['13']."</center></td>";
		echo "<td id='td14'> <center>".$this->board_value['14']."</center></td>";
	echo "</tr>";

	echo "<tr>";
		echo "<td id='td15'><center> ".$this->board_value['15']."</center></td>";
		echo "<td id='td16'> <center>".$this->board_value['16']."</center></td>";
		echo "<td id='td17'><center> ".$this->board_value['17']."</center></td>";
		echo "<td id='td18'> <center>".$this->board_value['18']."</center></td>";
		echo "<td id='td19'> <center>".$this->board_value['19']."</center></td>";
	echo "</tr>";

	echo "<tr>";
		echo "<td id='td20'> <center>".$this->board_value['20']."</center></td>";
		echo "<td id='td21'> <center>".$this->board_value['21']."</center></td>";
		echo "<td id='td22'><center> ".$this->board_value['22']."</center></td>";
		echo "<td id='td23'> <center>".$this->board_value['23']."</center></td>";
		echo "<td id='td24'> <center>".$this->board_value['24']."</center></td>";
	echo "</tr>";

echo "</table>";
	}
function show_form ()
{
echo "<form method='post' action='init_board1.php'>";
echo "<table>";
echo "<tr>";
	echo "<td>";
		echo "<label for='from'>Move sheep From : </label>";
	echo "</td>";
	echo "<td>";
		echo "<input name='from1' type='text' />";
	echo "</td>";
echo "</tr>";

echo "<tr>";
	echo "<td>";
		echo "<label for='to'>Move sheep To : </label>";
	echo "</td>";
	echo "<td>";
		echo "<input name='to1' type='text' />";
	echo "</td>";
echo "</tr>";
echo "<tr>";
	echo "<td></td>";
	echo "<td><input type='submit' value='Go' /></td>";
echo "</tr>";
echo "</form>";
echo "</table>";
}

function scan_board ()
{
public $empty_cell=array();
public $lion_position=array();
public $sheep_position=array();
for ($i=0;$i<25;$i++)
{
	if ($this->board_value[$i]==="")
	{
		array_push($empty_cell,$i);
		}
		else
		if ($this->board_value[$i]==="L")
		{
			array_push($lion_position,$i);
			}
			else
			if ($this->board_value[$i]==="S")
			{
				array_push($sheep_position,$i);
				}
				else
				echo "The index was out of bound";
	}
}
function move($from,$to)
{

$this->board_value[$to]="S";
}
}

?>

 

Suppose here in the "function show_form ()" user submits the form and i want form values to be used in the last function "function move()".  How to do that ?

im not sure what you mean but i suppose the main point was to get the form values to move function...

 

you can do that by calling the function in your 'init_board1.php' file which is the form action file. it would go like:

$b=new board;
$b->move($_REQUESt['from1'], $_REQUEST['to1']);

 

Truly hope that works... if doesnt please be more specific so i can try to help you out  :).

 

Nopes it is not working. Think my flow of calling functions are not ok. Please have a look at them. If possible please try code on your machine and if possible fix it. That would be generous of you.

include_once("init_board1.php");
$board_i=new board();
$board_i->init_board();
$board_i->show_board();
$board_i->show_form();
$board_i->move($_REQUEST['from1'],$_REQUEST['to1']);

you cant include your form action.. the move  method call should be contained in your in init_board1.php (which is your form action)...

if the code you gave is for the file: init_board1.php you should at least check there is request present(otherwise returns error)...

if(isset($_REQUEST['from1'])){
  $board_i->move($_REQUEST['from1'],$_REQUEST['to1']);
}

 

and the previous message:

$board_value['19']="S";

should contain index 0... (e.g. $board_value[0]['19']="S";)

 

can you pack your code and attach it and i will test it (pm or here)..

I am trying to make a board game. such as chess with all AI logic back there. But in php i am getting problems of syntax and logic. these are my steps for the game.

1-Initialize the board (with sheeps and lions at certain position) // this is done

2-scan board (for empty cells, lions position and sheep position)

3-user wants to move sheep from a state(cell) to other. // here i am getting problem

4-system will move lion based on AI principles.

 

This continues till end of game.

 

Hope you have got an idea what i am gonna do. If you need codes, I will send you. May be you can help me on this project.

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.