You can use a static variable :
function foo($val=null) {
static $var = null;
if (!is_null($var)) $var = $val;
return $val;
}
It is $vardisplayed only inside the function fooand is supported for several calls:
foo(123);
echo foo();
foo(456);
echo foo();
Or use a class with a private member and access / modify it using public methods:
class A {
private $var;
public function setVar($val) {
$this->var = $val;
}
public function getVar() {
return $this->var;
}
}
var :
$obj1 = new A();
$obj1->setVar(123);
$obj2 = new A();
$obj2->setVar(456);
echo $obj1->getVar();
echo $obj2->getVar();
static, , :
class A {
private static $var;
public function setVar($val) {
self::$var = $val;
}
public function getVar() {
return self::$var;
}
}
$obj1 = new A();
$obj1->setVar(123);
$obj2 = new A();
$obj2->setVar(456);
echo $obj1->getVar();
echo $obj2->getVar();