I am trying to modify an object that has been created by passing it from the class to another function. In my scenario below, I am would expect the result to be 2, but I am getting an "Uncaught Error".
------------------------------
Index.php
------------------------------
include "class.php";
include "function.php";
$number = new a();
$number->add(1);
echo $number->display();
$number->add_helper();
echo $number->display();
------------------------------
function.php
------------------------------
function get_help(&$numb) {
$numb->add(1);
}
------------------------------
class.php
------------------------------
class a {
protected $var;
public function add($numb) {
$this->var .= $numb;
}
public function display() {
return $this->var;
}
public function add_helper() {
get_help($this->var);
}
}
------------------------------
When I run the add_helper function, that is where errors happen. I an expecting the result to be 2.
Thanks in advance for the help.
Will
Thanks to Magnus Eriksson's response...
I was passing the string when I needed to be passing the object.
the add_helper function in my a class should actually be:
get_help($this);
instead of $this->var.
Thank you Magnus.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.