Viewed   167 times

e.g.:

$functions = array(
  'function1' => function($echo) { echo $echo; }
);

Is this possible? What's the best alternative?

 Answers

2

The recommended way to do this is with an anonymous function:

$functions = [
  'function1' => function ($echo) {
        echo $echo;
   }
];

If you want to store a function that has already been declared then you can simply refer to it by name as a string:

function do_echo($echo) {
    echo $echo;
}

$functions = [
  'function1' => 'do_echo'
];

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):

$functions = array(
  'function1' => create_function('$echo', 'echo $echo;')
);

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:

$functions['function1']('Hello world!');

call_user_func($functions['function1'], 'Hello world!');
Wednesday, November 16, 2022
4

Yes.

Memcache::set('someKey', array(
    'user_id' => 1,
    'url' => 'http://',
    'name' => 'Dave'
));

Please see the documentation for very detailed examples.

Thursday, December 1, 2022
2

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);
Friday, September 2, 2022
 
oded
 
5

No, you cannot do that directly. You have a few options:

  • Put every function in a separate php file and call the php file
  • use the first argument passed to the php file as the function name, and write a few lines of code to select the correct function.

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);
Tuesday, December 20, 2022
 
4

I hadn't noticed that you edited your question, which changes things entirely. You have two decent options here:

  1. Explicitly change them at the top of the function:

    function foo($bar, &$baz, &$qux) {
        $baz = $qux = array();
    }
    
  2. 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;
        }
    }
    
Thursday, September 1, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :