Действительно ли возможно приправить вызовы метода карри в PHP?

Просто установите "Value" и "Key" для dataValueField и dataTextField. Вы можете сделать это либо в самом View, либо в своих действиях (код ниже не тестировался).

var targets = new Dictionary<string, string>();
targets.Add("Key", "Value");

ViewData["MyList"] = new SelectList(targets, "Key", "Value");
14
задан Robert Munteanu 22 October 2009 в 21:12
поделиться

3 ответа

As of php 5.3 you can store an anonymous function in a variable. This anonymous function can call the "original" function with some predefined parameters.

function foo($x, $y, $z) {
  echo "$x - $y - $z";
}

$bar = function($z) {
  foo('A', 'B', $z);
};

$bar('C');

edit: You can also use a closure to parametrise the creation of the anonymous function

function foo($x, $y, $z) {
  echo "$x - $y - $z";
}

function fnFoo($x, $y) {
  return function($z) use($x,$y) {
    foo($x, $y, $z);
  };
}

$bar = fnFoo('A', 'B');
$bar('C');

edit2: This also works with objects

class Foo {
  public function bar($x, $y, $z) {
    echo "$x - $y - $z";
  }
}

function fnFoobar($obj, $x, $z) {
  return function ($y) use ($obj,$x,$z) {
    $obj->bar($x, $y, $z);
  };
}

$foo = new Foo;
$bar = fnFoobar($foo, 'A', 'C');
$bar('B');

But the other suggestions using __call() and a wrapper class may be better if you want to "enhance" a complete class.

21
ответ дан 1 December 2019 в 06:53
поделиться

PHP doesn't have currying per se, but you can do something like that in several ways. In your specific case, something like this may work:

class MySoapClient extends SoapClient {
  ...
  public function __call($meth,$args) {
    if (substr($method,0,5) == 'curry') {
      array_unshift($args,PASSWORD);
      array_unshift($args,USERNAME);
      return call_user_func_array(array($this,substr($meth,5)),$args);
    } else {
      return parent::__call($meth,$args);
    }
  }
}
$soapClient = new MySoapClient();
...
// now the following two are equivalent
$soapClient->currysomeMethod($additionalArg);
$soapClient->someMethod(USERNAME,PASSWORD,$additionalArg);

Although here's a more general solution for currying in PHP >= 5.3:

$curriedMethod = function ($additionalArg) use ($soapClient) { return $soapClient->method(USERNAME,PASSWORD,$additionalArg); }

$result = $curriedMethod('some argument');
3
ответ дан 1 December 2019 в 06:53
поделиться

Although not a very good solution, you could write a basic wrapper class that used PHPs magic methods (Specifically __call) to call the actual function but append the user name and password to the argument list.

Basic example:

class SC
{
    private $user;
    private $pass;

    public function __construct($user, $pass)
    {
        $this->user = $user;
        $this->pass = $pass;
    }

    public function __call($name, $arguments) 
    {
        $arguments = array_merge(array($this->user, $this->pass), $arguments);  
        call_user_func_array($name, $arguments);
    }
}
2
ответ дан 1 December 2019 в 06:53
поделиться
Другие вопросы по тегам:

Похожие вопросы: