I’m really stuck with references in php. In my programm i’ve got something similiar to this:
-
Programm
class Test { private $_property; function __construct($property) { $this->_property = $property; } public function setProperty($property) { $this->_property = $property; } public function getProperty() { return $this->_property; } } function doSmth(Test $var) { $newVar = new Test('test'); //I need to do something here... } $var = new Test('original'); doSmth($var); var_dump($var); -
Question
What should i do to copy all contents of $newVar variable to my $var variable so that i will be able to see it after using var_dump() function that is outside of function doSmth(). And i can’t use getters and setters in my programm because i’ve got a lot of them and it will be a lot of code. Is it possible to solve this problem with my limitations?
UPDATE: I can’t return value in my function doSmth() and i also tried __clone but nothing works. Can someone show me how can i do it with __clone()?
$newVaronly exists in the context of the function.What you want to do is to create a return value like this:
and call the function like this instead:
This way, your function returns the object of the class
Testas a a return value.Edit: If you cannot return a value, you can pass by reference instead:
This will modify the variable itself that is passed as a parameter to it (rather than passing a copy of it)