That's because you're moving the variable from the main scope to the function scope. When you're there, you import the VALUES (that is, make a COPY) of the variables supplised via the function call. If you change a variable inside the function, it will only be changed in the function scope, and not the main scope. To do that, you'll need to declare that variable to be global.

eg:
----------------
function addVal() {
  global $myarr;

  $myarr["valued"] = "d";

  var_dump($myarr);
}
--------
that would do what you're trying to do.

Brandon Goodin wrote:
Greetings,



This is a little difficult to explain. So, below is an example I will work
from. Forgive my ignorance here. I am a java developer using php4 and trying
to figure out a particular scenario.



In the following example when I run the myscript.php I pass the $myarr into
the addVal function of the MyClass class. The var_dump in the addVal
function appropriately displays all of the elements of the array a,b,c and
d. However, the var_dump that is done in the script immediately following
the addVal method call displays only elements a,b and c with NO d. From what
I can see, php does not retain the same reference to the array object when
it passes it into the function. It appears that it clones the array and
retains only the changes within the scope of the function. In java I am used
to creating an array and passing it into a method which accomplishes
operations against the array. The passed in array is also modified because
it is a reference to the same array object.



Is there a way to accomplish this behavior in php 4?



For example:



==== myscript.php ====



.



$myarr = array();



$myarr["valuea"]="a";

$myarr["valueb"]="b";

$myarr["valuec"]="c";



$myclass = new MyClass();

$myclass->addVal($myarr);



var_dump($myarr);



.



=======end ========







==== myclass.php ====



class MyClass {



function addVal($myarr){

  $myarr["valued"] = "d";

  var_dump($myarr);

}



}

=======end ========





Thanks,

Brandon





-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to