e.g.:
$functions = array(
'function1' => function($echo) { echo $echo; }
);
Is this possible? What's the best alternative?
e.g.:
$functions = array(
'function1' => function($echo) { echo $echo; }
);
Is this possible? What's the best alternative?
Yes.
Memcache::set('someKey', array(
'user_id' => 1,
'url' => 'http://',
'name' => 'Dave'
));
Please see the documentation for very detailed examples.
what you mean its call : Passing by Reference
its very simple like
function changearray(&$arr){
$arr['x'] = 'y';
}
you can call this like :
changearray($_SESSION);
No, you cannot do that directly. You have a few options:
Update:
Here is a example of using the first passed parameter as a function call:
if(function_exists( $argv[1] ))
call_user_func_array($argv[1], $argv);
I hadn't noticed that you edited your question, which changes things entirely. You have two decent options here:
Explicitly change them at the top of the function:
function foo($bar, &$baz, &$qux) {
$baz = $qux = array();
}
Perhaps this is a good place to introduce an object?
class parseConfig {
protected $rawXml;
protected $configName;
protected $radioArr = array();
protected $flasherArr = array();
protected $irdArr = array();
public function __construct($raw_xml = null) {
if (!is_null($raw_xml)) {
$this->rawXml = $raw_xml;
$this->parse();
}
}
public function parse($raw_xml = null) {
if (!is_null($raw_xml)) {
$this->rawXml = $raw_xml;
}
if (empty($this->rawXml)) {
return null;
}
$this->configName = '';
$this->radioArr =
$this->flasherArr =
$this->irdArr = array();
// parsing happens here, may return false
return true;
}
public function setRawXml($raw_xml) {
$this->rawXml = $raw_xml;
return $this;
}
public function getRawXml() {
return $this->rawXml;
}
public function getRadioArr() {
return $this->radioArr;
}
public function getFlasherArr() {
return $this->flasherArr;
}
public function getIrdArr() {
return $this->irdArr;
}
}
The recommended way to do this is with an anonymous function:
If you want to store a function that has already been declared then you can simply refer to it by name as a string:
In ancient versions of PHP (<5.3) anonymous functions are not supported and you may need to resort to using
create_function
(deprecated since PHP 7.2):All of these methods are listed in the documentation under the
callable
pseudo-type.Whichever you choose, the function can either be called directly (PHP ?5.4) or with
call_user_func
/call_user_func_array
: