Can you do something crazy like this
function cool_function($pizza, $ice_cream) {
make the arguments in the array into an array
return $array_of_paramaters // my new array
} // end function
print_r(cool_function); // outputs array(0 => pizza, 1=> ice cream)
Actually, this is pretty easy (and read the manual: func_get_args — Returns an array containing a function's argument list, see as well: Variable-length argument lists):
function cool_function($pizza, $ice_cream) {
return func_get_args();
}
but as asked in comments, why do you need this?
Or do you need the variable names? Reflection Docs is your friend:
function cool_named($neutron, $electron)
{
$f = new ReflectionFunction(__FUNCTION__);
$p = array();
foreach($f->getParameters() as $p1)
$p[] = '$'.$p1->name;
return $p;
}
var_dump(cool_named());
Or just both? (Edit: taking under-length, optional and over-length parameters into account):
function overall($neutron, $electron, $quark = 'xiaro')
{
$f = new ReflectionFunction(__FUNCTION__);
$defined = $f->getParameters();
$passed = func_get_args() + array_fill(0, count($defined), NULL);
foreach($defined as &$param)
$param = '$'.$param->name;
return array_combine($defined + array_keys($passed), $passed);
}
var_dump(overall('clara', 'peter', 'moon', 'jupiter'));
I'm not sure if you're looking for func_get_args which return all arguments passed to a function, or the ReflectionFunction class.
A basic example of func_get_args:
function cool_function($pizza, $ice_cream) {
return func_get_args();
}
But you don't need the arguments to make this work:
function cool_function() {
return func_get_args();
}
// cool_function(1,2,3) will return array(1,2,3)
The reflection option:
/**
* Returns an array of the names of this function
*/
function getMyArgNames($a,$b,$c)
{
$args = array();
$refFunc = new ReflectionFunction(__FUNCTION__);
foreach( $refFunc->getParameters() as $param ){
$args[] = $param->name;
}
return $args;
}
Or, for the really crazy:
/**
* Returns an associative array of the names of this function mapped
* to their values
*/
function getMyArgNames($a,$b,$c)
{
$received = func_get_args();
$i = 0;
$args = array();
$refFunc = new ReflectionFunction(__FUNCTION__);
foreach( $refFunc->getParameters() as $param ){
$args[$param->name] = $received[$i++];
}
// include all of those random, optional arguments.
while($i < count($received)) $args[] = $received[$i++];
return $args;
}
do you mean
function valuesToArray($value1,$value2){return array($value1,$value2);}
If I got you right, you so it like this:
function cool_function($pizza, $ice_cream) {
return array($pizza, $ice_cream);
}
but you could simply do that:
$new_var = array($pizza, $ice_cream);
Related
How to refactor this snippet of code, to reduce indentation level by one?
I just wonder is it possible in PHP to write this code in a diffrent way, with just one level of indentation.
The code:
private function isArrayMatchingCriteria(array $array) {
foreach($array as $element) {
if (! $this->isElementMatchingCriteria($element) {
return false;
}
}
return true;
}
Please take into consideration, that:
this code doesn't always iterate over all array elements - so combination of count + array_filter / array_map isn't the same
it is easy to do by introducing a dedicated object attribute serving as a flag, but I'm looking for a way without introducing new attributes
If you're just looking to remove indentation, you could use:
private function isArrayMatchingCriteria(array $array) {
foreach($array as $element) {
if (!$this->isElementMatchingCriteria($element)) return false;
}
return true;
}
Use array_map, something like this:
class MyClass
{
private function isElementMatchingCriteria( $element )
{
// DUMMY, replace with actual code
if ( $element == "foo" || $element == "bar" ) {
return true;
} else {
return false;
}
} // end is Element Matching Criteria
public function isArrayMatchingCriteria(array $array)
{
$results = array_map( array( $this, "isElementMatchingCriteria"), $array );
$isMatch = true;
foreach ( $results as $result ) {
$isMatch = $isMatch && $result;
} // end foreach
return $isMatch;
} // end function isArrayMatchingCriteria
} // end MyClass
$myClass = new MyClass();
$array = array( "foo", "bar", "baz" );
$result = $myClass->isArrayMatchingCriteria( $array );
print_r( $result );
I'm trying to write a memoization function, and I just realized this solution does not work when the callback is not a simple string. For example, PHP can accept a callback in the form array($this,'memberFunc'), which is not amenable to serialization.
Then I realized that we don't really want to hash/serialize the whole callback function/object anyway, we just need a unique ID for it so we can check for reference equality. I thought spl_object_hash would do the trick, but it doesn't work on arrays.
Is there another way to generate a unique reference ID for a callable?
You can always cast to object for hashing purposes:
<?php
class Foo{
public function __construct(){
$foo = array($this,'memberFunc');
var_dump( spl_object_hash((object)$foo) );
var_dump( spl_object_hash((object)$foo) );
}
}
new Foo;
string(32) "00000000532ba9fd000000000cc9b0a5"
string(32) "00000000532ba9fd000000000cc9b0a5"
I wrote this function to get a hash for callables specifically:
/**
* #param callable $func
* #return string Unique string identifier for callable instance
* #throws Exception
*/
private static function callable_hash($func) {
if(!is_callable($func)) throw new Exception("Function must be a callable");
if(is_string($func)) {
return $func;
} elseif(is_array($func)) {
if(count($func) !== 2) throw new Exception("Array-callables must have exactly 2 elements");
if(!is_string($func[1])) throw new Exception("Second element of array-callable must be a string function name");
if(is_object($func[0])) return spl_object_hash($func[0]).'::'.$func[1];
elseif(is_string($func[0])) return implode('::',$func);
throw new Exception("First element of array-callable must be a class name or object instance");
} elseif(is_object($func)) {
return spl_object_hash($func);
}
throw new Exception("Unhandled callable type");
}
But if Alvaro's solution works... that's much simpler and more flexible.
Based on Alvaro's answer, I came up with these two functions:
function memoized() {
static $cache = array();
$args = func_get_args();
$func = array_shift($args);
$key = sha1(serialize(array_merge(array(spl_object_hash((object)$func)),$args)));
if(!isset($cache[$key])) {
$cache[$key] = call_user_func_array($func, $args);
}
return $cache[$key];
}
function memoized_func($func) {
return function() use ($func) {
static $cache = array();
$args = func_get_args();
$key = sha1(serialize($args));
if(!isset($cache[$key])) {
$cache[$key] = call_user_func_array($func, $args);
}
return $cache[$key];
};
}
Usage:
$f = function($n) {
for($i=0; $i<$n; ++$i);
return $n;
};
$result = memoized($f,50000);
$func = memoized_func($f);
$result2 = $func(50000);
I have a wrapper class for my $_SESSION array because I like to work with objects and it sort of prevents me from getting lazy and calling the super global from places I should not.
It used to be ok to have a simple:
public function get($name);
public function set($name, $value);
public function exists($name);
......
but now I am implementing a shopping service and it has a shopping cart and when the user adds an item to the cart it sets it like $_SESSION['cart'][$productId] which holds the quantity so as you can see my get() and set() break down.
I currently have this for my new get()
/*
* #args: ...
* #return: mixed
*/
public function get() {
$keys = func_get_args();
$value = $_SESSION[array_shift($keys)];
foreach( $keys as $key ) {
$value = $value[$key];
}
return $value;
}
// This is how I use it then
$quantity = $session->get('cart', $productId);
It works with perfectly assuming the keys being search for do exist, otherwise it gives me a warning.
The problem now is the set() method. I want to do it in a similar fashion so any amount of keys can be given in the signature of the method and then the value to store but it is proving to be very confusing for me anyway.
Does anyone know how to accomplish this?
Thanks.
This should work:
class Session{
// $...
public function get(){
$keys = func_get_args();
if(count($keys) < 1){
// handle exception
}
$value = $_SESSION[array_shift($keys)];
foreach($keys as $key){
if(!isset($value[$key])){
// handle exception
}
$value = $value[$key];
}
return $value;
}
// $valueToSet, $...
public function set(){
$data = func_get_args();
if(count($data) < 2){
// handle exception
}
$val = array_shift($data);
$value = &$_SESSION[array_shift($data)];
foreach($data as $key){
if(!isset($value[$key])){
$value[$key] = array();
}
$value = &$value[$key];
}
$value = $val;
}
// $...
public function exists(){
$keys = func_get_args();
if(count($keys) < 1){
// handle exception
}
$tmp = array_shift($keys);
if(!isset($_SESSION[$tmp])) return false;
$value = $_SESSION[$tmp];
foreach($keys as $key){
if(!isset($value[$key])){
return false;
}
$value = $value[$key];
}
return true;
}
}
I can give you a little example:
$var = 'asdf';
$var_copy = $var;
$var_copy2 = &$var;
$var_copy = 'asdf2';
echo $var; // prints 'asdf'
$var_copy2 = 'asdf2';
echo $var; // prints 'asdf2'
And a little link from php.net
I would like to bind a variable to a function's scope, I can do this in php use the 'use' keyword after PHP 5.3, however how do I do the equivalent in versions < PHP 5.3?
test_use_keyword();
function test_use_keyword(){
$test =2;
$res=array_map(
function($el) use ($test){
return $el * $test;
},
array(3)
);
print_r($res);
}
You can use a global variable, but you should always avoid globals variables whereever possible. As a suggestion, without knowing, what you are trying to solve with this
class Xy ( {
private $test;
public function __construct ($test) {
$this->test = $test;
}
public function call ($el) {
return $el * $this->test;
}
}
print_r(array_map(array(new Xy(2), 'call'), array(3));
Also possible are the good old lambdas
$test = 2;
$a = create_function ('$el', 'return $el * ' . $test . ';');
print_r (array_map($a, array(3)));
Normally through globals, seriously. Although hacks could be used to mimic the functionality, like partial functions in php. Extracted from article:
function partial()
{
if(!class_exists('partial'))
{
class partial{
var $values = array();
var $func;
function partial($func, $args)
{
$this->values = $args;
$this->func = $func;
}
function method()
{
$args = func_get_args();
return call_user_func_array($this->func, array_merge($args, $this->values));
}
}
}
//assume $0 is funcname, $1-$x is partial values
$args = func_get_args();
$func = $args[0];
$p = new partial($func, array_slice($args,1));
return array($p, 'method');
}
And only after that could you have something like.
function multiply_by($base, $value) {
return $base * $value;
}
// ...
$res = array_map(partial("multiply_by", $test), array(3));
Not... worth... it.
PHP functions such as 'array_map' take a callback, which can be a simple function or a class or object method:
$array2 = array_map('myFunc', $array);
or
$array2 = array_map(array($object, 'myMethod'), $array);
But is there a syntax to pass a method which is bound within the iteration to the current object (like 'invoke' in Prototype.js)? So that the following could be used:
$array2 = array_map('myMethod', $array);
with the effect of
foreach($array as $obj) $array2[] = $obj->myMethod();
Obviously I can use this form, or I can write a wrapper function to make the call, and even do that inline. But since 'myMethod' is already a method it seems to be going round the houses to have to do one of these.
Not currently. When php 5.3 comes out, you could use the following syntax:
$array2 = array_map(function($obj) { return $obj->myMethod(); }, $array);
function obj_array_map($method, $arr_of_objects) {
$out = array();
$args = array_slice(func_get_args(), 2);
foreach ($arr_of_objects as $key => $obj) {
$out[$key] = call_user_func_array(Array($obj, $method), $args);
}
return $out;
}
// this code
$a = Array($obj1, $obj2);
obj_array_map('method', $a, 1, 2, 3);
// results in the calls:
$obj1->method(1, 2, 3);
$obj2->method(1, 2, 3);
Basically, no. There is no special syntax to make this any easier.
I can think of a fancier way of doing this in PHP 5.3, seeing as there's always more than one way to do things in PHP, but I'd say it wouldn't necessarily be better than your foreach example:
$x = array_reduce(
$array_of_objects,
function($val, $obj) { $val = array_merge($val, $obj->myMethod()); return $val; },
array()
);
Just go with your foreach :)
<?php
// $obj->$method(); works, where $method is a string containing the name of the
// real method
function array_map_obj($method, $array) {
$out = array();
foreach ($array as $key => $obj)
$out[$key] = $obj->$method();
return $out;
}
// seems to work ...
class Foo {
private $y = 0;
public function __construct($x) {
$this->y = $x;
}
public function bar() {
return $this->y*2;
}
}
$objs = array();
for ($i=0; $i<20; $i++)
$objs[] = new Foo($i);
$res = array_map_obj('bar', $objs);
var_dump($res);
?>
voila!
This is a bit of a silly answer, but you could subclass ArrayObject and use that instead of a normal array:
<?php
class ArrayTest extends ArrayObject {
public function invokeMethod() {
$result = array();
$args = func_get_args();
$method = array_shift($args);
foreach ($this as $obj) {
$result[] = call_user_func_array(array($obj, $method), $args);
}
return $result;
}
}
//example class to use
class a {
private $a;
public function __construct($a) {
$this->a = $a;
}
public function multiply($n) {
return $this->a * $n;
}
}
//use ArrayTest instance instead of array
$array = new ArrayTest();
$array[] = new a(1);
$array[] = new a(2);
$array[] = new a(3);
print_r($array->invokeMethod('multiply', 2));
Outputs this:
Array
(
[0] => 2
[1] => 4
[2] => 6
)
I would use create_function() to ... well ... create a temporary function for array_map while waiting for PHP 5.3
$func = create_function('$o', '$o->myMethod();');
array_map($func, $objects);