473,320 Members | 1,600 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Pass by reference question

I am creating a program to create sudoku games. I have an object that
is 1 row of a sudoku game. I store the initial state of the row before
I start figuring out the values to place in each cell, in case the
program runs into a problem and has to revert the row back to its
original state.

So first I store the initial State of the row, then I start to modify
the row. My problem is that once I start to change the original row,
it also changes the initial state that I saved. The only thing I can
think that is doing this is that instead of creating a copy of the
object's data in my initial state variable, it is just changing its
pointer to the original rows location.

I wrote a smaller test case to show what I am talking about. At the
end of the program, each cell in the initial state should be full of
numbers 1 to 9. Instead, it is a copy of the current state of the row.
Hopefully someone can clue me in on what I am missing? You can see the
output of the program here
http://mustang.millersville.edu/~ari...udoku/test.php

Thanks again for the help!!

<?php
################################################## #########

class Cell {
public $c = array(1,2,3,4,5,6,7,8,9);

//__________________________________________________ ___

public function getCandidates() {
return $this->c;
}

//__________________________________________________ ___

public function pickRandomCandidate() {
return $this->c[rand() % count($this->c)];
}

//__________________________________________________ ___

public function setCandidate($a) {
$this->c = array($a);
}

//__________________________________________________ ___

public function removeCandidateFromCell($v) {
$this->c = array_values(array_diff($this->c,array($v)));
}
}

################################################## #########

class Row {
public $cells = array();
public $initialState = array();

//__________________________________________________ ___

public function __construct() {
for ($i=0;$i<9;$i++)
$this->cells[$i] = new Cell();
}

//__________________________________________________ ___

public function getInitialState() {
return $this->initialState;
}

public function getCurrentState() {
return $this->cells;
}

//__________________________________________________ ___

public function setInitialState() {
$this->initialState = $this->getCurrentState();
}

//__________________________________________________ ___

public function printRow() {
echo "<strong>Printing Row State</strong>";
echo "<table border=1 cellpadding=4>";
echo "<tr>";
foreach($this->cells as $cell) {
echo "<td align='center'>";
for($i=1; $i <= 9; $i++) {
echo (in_array($i, $cell->c)) ? $i : "<span style='color:
#ddd'>$i</span>";
echo $i==3 || $i==6 ? "<br />" : "";
}
echo "</td>";
}
echo "</tr>";

echo"</table>";
}

//__________________________________________________ ___

public function printInitialState($rowNum = "") {
echo "<strong>Initial State of $rowNum</strong>";
echo "<table border=1 cellpadding=4>";
echo "<tr>";
foreach($this->initialState as $cell) {
echo "<td align='center'>";
for($i=1; $i <= 9; $i++) {
echo (in_array($i, $cell->c)) ? $i : "<span style='color:
#ddd'>$i</span>";
echo $i==3 || $i==6 ? "<br />" : "";
}
echo "</td>";
}
echo "</tr>";
echo"</table>";
}
//__________________________________________________ ___

public function resetRow($rowNum = "") {
echo "resetting row $rowNum <br />";
$this->cells = $this->getInitialState();
#$this->cells = $this->initialState;
}

//__________________________________________________ ___

public function removeCandidateFromRow($v) {
foreach($this->cells as $c =$val) {
$val->removeCandidateFromCell($v);
}
}
}

$myrow = new Row();

$myrow->setInitialState();

$candidate = $myrow->cells[0]->pickRandomCandidate();

$myrow ->removeCandidateFromRow($candidate);
$myrow->cells[0]->setCandidate($candidate);

$myrow->printRow();
$myrow->printInitialState();
?>

Oct 10 '06 #1
3 1532
Hi,

In PHP4, objects were handled using copy-on-write semantics. In PHP5,
this is no longer the case. In this code, you would probably want to
make a copy of the cell objects using the clone operator, instead of
referencing them. For instance:

public function setInitialState() {
// Clear the target array
$this->initialState = array();
// Iterate over the source objects
foreach ($this->getCurrentState() as $cell) {
// Use the clone operator to copy the object:
$this->initialState[] = clone $cell;
}
}

Some more info on cloning in PHP5:

http://us2.php.net/manual/en/language.oop5.cloning.php

The clone operator will by default create a shallow copy of an object.
(You can change this behavior by implementing a __clone method in
Cell.)

FYI, here's an interesting look into PHP variable management:
http://www.quepublishing.com/article...&seqNum=2&rl=1

Adam wrote:
I am creating a program to create sudoku games. I have an object that
is 1 row of a sudoku game. I store the initial state of the row before
I start figuring out the values to place in each cell, in case the
program runs into a problem and has to revert the row back to its
original state.

So first I store the initial State of the row, then I start to modify
the row. My problem is that once I start to change the original row,
it also changes the initial state that I saved. The only thing I can
think that is doing this is that instead of creating a copy of the
object's data in my initial state variable, it is just changing its
pointer to the original rows location.

I wrote a smaller test case to show what I am talking about. At the
end of the program, each cell in the initial state should be full of
numbers 1 to 9. Instead, it is a copy of the current state of the row.
Hopefully someone can clue me in on what I am missing? You can see the
output of the program here
http://mustang.millersville.edu/~ari...udoku/test.php

Thanks again for the help!!

<?php
################################################## #########

class Cell {
public $c = array(1,2,3,4,5,6,7,8,9);

//__________________________________________________ ___

public function getCandidates() {
return $this->c;
}

//__________________________________________________ ___

public function pickRandomCandidate() {
return $this->c[rand() % count($this->c)];
}

//__________________________________________________ ___

public function setCandidate($a) {
$this->c = array($a);
}

//__________________________________________________ ___

public function removeCandidateFromCell($v) {
$this->c = array_values(array_diff($this->c,array($v)));
}
}

################################################## #########

class Row {
public $cells = array();
public $initialState = array();

//__________________________________________________ ___

public function __construct() {
for ($i=0;$i<9;$i++)
$this->cells[$i] = new Cell();
}

//__________________________________________________ ___

public function getInitialState() {
return $this->initialState;
}

public function getCurrentState() {
return $this->cells;
}

//__________________________________________________ ___

public function setInitialState() {
$this->initialState = $this->getCurrentState();
}

//__________________________________________________ ___

public function printRow() {
echo "<strong>Printing Row State</strong>";
echo "<table border=1 cellpadding=4>";
echo "<tr>";
foreach($this->cells as $cell) {
echo "<td align='center'>";
for($i=1; $i <= 9; $i++) {
echo (in_array($i, $cell->c)) ? $i : "<span style='color:
#ddd'>$i</span>";
echo $i==3 || $i==6 ? "<br />" : "";
}
echo "</td>";
}
echo "</tr>";

echo"</table>";
}

//__________________________________________________ ___

public function printInitialState($rowNum = "") {
echo "<strong>Initial State of $rowNum</strong>";
echo "<table border=1 cellpadding=4>";
echo "<tr>";
foreach($this->initialState as $cell) {
echo "<td align='center'>";
for($i=1; $i <= 9; $i++) {
echo (in_array($i, $cell->c)) ? $i : "<span style='color:
#ddd'>$i</span>";
echo $i==3 || $i==6 ? "<br />" : "";
}
echo "</td>";
}
echo "</tr>";
echo"</table>";
}
//__________________________________________________ ___

public function resetRow($rowNum = "") {
echo "resetting row $rowNum <br />";
$this->cells = $this->getInitialState();
#$this->cells = $this->initialState;
}

//__________________________________________________ ___

public function removeCandidateFromRow($v) {
foreach($this->cells as $c =$val) {
$val->removeCandidateFromCell($v);
}
}
}

$myrow = new Row();

$myrow->setInitialState();

$candidate = $myrow->cells[0]->pickRandomCandidate();

$myrow ->removeCandidateFromRow($candidate);
$myrow->cells[0]->setCandidate($candidate);

$myrow->printRow();
$myrow->printInitialState();
?>
Oct 10 '06 #2
Hmm Adam <ar*****@gmail.comwrote:
Hopefully someone can clue me in on what I am missing? You can see the

user __clone method for object

--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ikciu | gg: 718845 | yahoo: ikciu_irsa | www: www.e-irsa.pl

2be || !2be $this =mysql_query();
Oct 10 '06 #3
Exactly what I needed, thank you!

pe*******@gmail.com wrote:
Hi,

In PHP4, objects were handled using copy-on-write semantics. In PHP5,
this is no longer the case. In this code, you would probably want to
make a copy of the cell objects using the clone operator, instead of
referencing them. For instance:

public function setInitialState() {
// Clear the target array
$this->initialState = array();
// Iterate over the source objects
foreach ($this->getCurrentState() as $cell) {
// Use the clone operator to copy the object:
$this->initialState[] = clone $cell;
}
}

Some more info on cloning in PHP5:

http://us2.php.net/manual/en/language.oop5.cloning.php

The clone operator will by default create a shallow copy of an object.
(You can change this behavior by implementing a __clone method in
Cell.)

FYI, here's an interesting look into PHP variable management:
http://www.quepublishing.com/article...&seqNum=2&rl=1

Adam wrote:
I am creating a program to create sudoku games. I have an object that
is 1 row of a sudoku game. I store the initial state of the row before
I start figuring out the values to place in each cell, in case the
program runs into a problem and has to revert the row back to its
original state.

So first I store the initial State of the row, then I start to modify
the row. My problem is that once I start to change the original row,
it also changes the initial state that I saved. The only thing I can
think that is doing this is that instead of creating a copy of the
object's data in my initial state variable, it is just changing its
pointer to the original rows location.

I wrote a smaller test case to show what I am talking about. At the
end of the program, each cell in the initial state should be full of
numbers 1 to 9. Instead, it is a copy of the current state of the row.
Hopefully someone can clue me in on what I am missing? You can see the
output of the program here
http://mustang.millersville.edu/~ari...udoku/test.php

Thanks again for the help!!

<?php
################################################## #########

class Cell {
public $c = array(1,2,3,4,5,6,7,8,9);

//__________________________________________________ ___

public function getCandidates() {
return $this->c;
}

//__________________________________________________ ___

public function pickRandomCandidate() {
return $this->c[rand() % count($this->c)];
}

//__________________________________________________ ___

public function setCandidate($a) {
$this->c = array($a);
}

//__________________________________________________ ___

public function removeCandidateFromCell($v) {
$this->c = array_values(array_diff($this->c,array($v)));
}
}

################################################## #########

class Row {
public $cells = array();
public $initialState = array();

//__________________________________________________ ___

public function __construct() {
for ($i=0;$i<9;$i++)
$this->cells[$i] = new Cell();
}

//__________________________________________________ ___

public function getInitialState() {
return $this->initialState;
}

public function getCurrentState() {
return $this->cells;
}

//__________________________________________________ ___

public function setInitialState() {
$this->initialState = $this->getCurrentState();
}

//__________________________________________________ ___

public function printRow() {
echo "<strong>Printing Row State</strong>";
echo "<table border=1 cellpadding=4>";
echo "<tr>";
foreach($this->cells as $cell) {
echo "<td align='center'>";
for($i=1; $i <= 9; $i++) {
echo (in_array($i, $cell->c)) ? $i : "<span style='color:
#ddd'>$i</span>";
echo $i==3 || $i==6 ? "<br />" : "";
}
echo "</td>";
}
echo "</tr>";

echo"</table>";
}

//__________________________________________________ ___

public function printInitialState($rowNum = "") {
echo "<strong>Initial State of $rowNum</strong>";
echo "<table border=1 cellpadding=4>";
echo "<tr>";
foreach($this->initialState as $cell) {
echo "<td align='center'>";
for($i=1; $i <= 9; $i++) {
echo (in_array($i, $cell->c)) ? $i : "<span style='color:
#ddd'>$i</span>";
echo $i==3 || $i==6 ? "<br />" : "";
}
echo "</td>";
}
echo "</tr>";
echo"</table>";
}
//__________________________________________________ ___

public function resetRow($rowNum = "") {
echo "resetting row $rowNum <br />";
$this->cells = $this->getInitialState();
#$this->cells = $this->initialState;
}

//__________________________________________________ ___

public function removeCandidateFromRow($v) {
foreach($this->cells as $c =$val) {
$val->removeCandidateFromCell($v);
}
}
}

$myrow = new Row();

$myrow->setInitialState();

$candidate = $myrow->cells[0]->pickRandomCandidate();

$myrow ->removeCandidateFromRow($candidate);
$myrow->cells[0]->setCandidate($candidate);

$myrow->printRow();
$myrow->printInitialState();
?>
Oct 10 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
7
by: Zlatko Matić | last post by:
Let's assume that we have a database on some SQL server (let it be MS SQL Server) and that we want to execute some parameterized query as a pass.through query. How can we pass parameters to the...
4
by: z_learning_tester | last post by:
I'm reading the MS press C# book and there seems to be a contradiction. Please tell me which one is correct, 1 or 2. Thanks! Jeff 1. First it gives the code below saying that it prints 0 then...
10
by: John Bailo | last post by:
I want to pass a SqlCommand object as a input parameter to a method. I want to pass the SqlCommand "by value" so that any updates to the original object are *not* reflected in the object within...
3
by: TeejB | last post by:
Hi all, Quick question: If I have a function which populates a large array (ie. reading rowsets), is it better to pass in a reference to a variable to accept the data, or should I just create,...
4
by: kinaxx | last post by:
Hello, now I'm learning progamming language in university. but i have some question. in textbook. says there are four passing Mechanism 1) pass by value (inother words : call by value) 2)...
11
by: kinaxx | last post by:
Hello, now I'm learning progamming language in university. but i have some question. in textbook. says there are four passing Mechanism 1) pass by value (inother words : call by value) 2)...
15
by: ramif | last post by:
Does call by reference principle apply to pointers?? Is there a way to pass pointers (by reference) to functions? Here is my code: #include <stdio.h> #include <stdlib.h>
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.