In javascript I know i can like so:
Code: Select all
myFunction(function () {
//blcok of code
});
Thanks for reading, I hope it's more simple then I might think.
Moderator: General Moderators
Code: Select all
myFunction(function () {
//blcok of code
});
Code: Select all
<?php
function bar($x) {
echo $x*2;
}
function foo($fn) {
$fn(1234);
}
foo('bar');Well, I'm really looking for a way to pass a handle that will be executed in the function. Err. For example, myFunction has one parameter, handle, and you pass in a block of code to be executed in myFunction. The only way to pass in blocks of code in javascript is through using a function. But sense you can really create functions as fluently in php is there another way to pass multiple statements to be executed?volka wrote:You can pass the function's name.Code: Select all
<?php function bar($x) { echo $x*2; } function foo($fn) { $fn(1234); } foo('bar');
Code: Select all
database->each(array("query" => "SELECT...","callback" => /*passed in code*/));
Code: Select all
$fn = create_function('$x', 'return $x;');
var_dump($fn);string(9) "
lambda_1"
Hmm. I don't understand? You mean just define a function then pass it like you said before? The only problem with this is that I have this function that I defined left over. If I could create the function within the same statement like:volka wrote:'callback'=>'nameOfFunction'
Even if you use create_function() the return value will be the name of the newly created function as string.Code: Select all
$fn = create_function('$x', 'return $x;'); var_dump($fn);string(9) "
lambda_1"
Code: Select all
database->each(array("callback" => function foo(){
//functions code
}));
Code: Select all
function foo() {
//functions code
}
database->each(array("callback" => 'foo'));
unset('foo');
Code: Select all
myobject.mymethod(function () {
//code
});
Code: Select all
myobject->mymethod(function () {
//code
});
Code: Select all
function foo()
{
//code
}
myobject->mymethod('foo');
Code: Select all
function foo()
{
//code
}
myobject->mymethod('foo');
function foo()
{
//deferent code
}
myobject->mymethod('foo');
Code: Select all
myobject->mymethod(function () {
//code
});
Code: Select all
class MyClass {
function mymethod($command) {
$command->foo()
}
}
$myobject = new MyClass();
class Bar1 {
function foo() {
//code
}
}
$myobject->mymethod(new Bar1($some, $params));
class Bar2 {
function foo() {
//deferent code
}
}
$myobject->mymethod(new Bar2($other, $params));Code: Select all
class object {
public function method($funcName) {
echo $funcName(1, 3); // should output 4
}
}
$function = create_function('$a, $b', "return (int)\$a+(int)\$b;");
$object = new object();
$object->method($function);In a way yes. But the create_function doesn't appear to be very..."good". Not like javascript which uses, I guess you say, closure or lambda style programming.vigge89 wrote:I'm guessing this is what you want?Code: Select all
class object { public function method($funcName) { echo $funcName(1, 3); // should output 4 } } $function = create_function('$a, $b', "return (int)\$a+(int)\$b;"); $object = new object(); $object->method($function);