add arguments to function's name in php? [duplicate] - php

This question already has answers here:
Closed 11 years ago.
actually I wish to create a function which combine all similar functions with similar function name. For example,
<?php
//this function carry the function name I wish to create. For example:function try
a("try");
function a($arg)
{
//I wish to create function with the name carried by $arg
//but function $arg won't work
}
?>
Anyone can guide me regarding such function?

PHP is able to use variables as call-time function names so all you would have to do is
function a($arg)
{
if (!function_exists($arg)) {
throw new InvalidArgumentException(
sprintf('"%s" is not a valid function', $arg));
}
$arg();
}
If you must go down the functional path, you should do it properly
function a(Closure $callback)
{
$callback();
}
a(function() {
echo 'This is a callback';
});
or even
$b = function() {
echo 'I am an anonymous function';
};
a($b);
See http://php.net/manual/en/functions.anonymous.php

Related

Passing parameters in a function inside another function? [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 6 years ago.
I m searching for some help
It is possible to set a function with Arguments/Parameters inside another function in php? Here i have a theoretical example.
<?php
// Function with parameter/s
function funcOne($arg) {
return $arg;
}
// Parameter/s inside another function.. Possible!?
function funcTwo() {
return funcOne($arg);
}
When i try to set the parameter like this
funcOne('Alex');
echo funcTwo();
I get the following notice error
Notice: Undefined variable: arg in...
Thanks in advance :)
// Function with parameter/s
function funcOne($arg) {
return $arg;
}
// Parameter/s inside another function.. Possible!?
function funcTwo() {
return funcOne($arg);
}
funcOne('Alex');
//Call is made to function one which returned an ARG.
NOTE that here the function just returned the arg and forgot about it, now that argument is NO WHERE stored to be used
//Now here inside the functionTwo scope $arg is never defined.
echo funcTwo();
You may do the following using classes and objects
class MyClass {
public $classarg;
public function funcOne($arg) {
$this->classarg = $arg; //assigned the argument to a class variable
}
function funcTwo() {
return $this->classarg; //using the class variable to test
}
}
$myobj = new MyClass();
$myobj->funcOne('Alex');
echo $myobj->funcTwo()
You can also use global variable to achieve what you want, but I will NOT recommend to use it as Object Oriented Programming is what we should be using going forward
funcOne('Alex')is not setting a parameter, it is calling the function funcOne().
When funcOne($arg) executes, it returns the parameter $arg to the caller.
echo funcOne('Alex') will echo Alex, because that is the value returned.
After return, funcOne does not know about 'Alex' any more.
when you call funcTwo(), it executes funcOne($arg), but $arg is not defined: it has no value assigned.
function funcTwo($arg) {
return funcOne($arg);
}
Note that you should learn to use variables before making functions.

PHP - Can you assign a member function to a variable? [duplicate]

This question already has answers here:
php call class function by string name
(6 answers)
Closed 8 years ago.
In PHP5, variables can be evaluated as functions1 such as:
function myFunc() {
echo "whatever";
}
$callableFunction = 'myFunc';
$callableFunction(); // executes myFunc()
Is there any syntax for assigning object member functions to a variable such as:
class MyClass {
function someCall() {
echo "yay";
}
}
$class = new MyClass();
// what I would like:
$assignedFunction = $class->someCall; // but I tried and it returns an error
$memberFunc = 'someCall';
$class->$memberFunc(); // I know this is valid, but I want a single variable to be able to be used to call different functions - I don't want to have to know whether it is part of a class or not.
// my current implementation because I don't know how to do it with anonymous functions:
$assignedFunction = function() { return $class->someCall(); } // <- seems lengthy; would be more efficient if I can just assign $class->someCall to the variable somehow?
$assignedFunction(); // I would like this to execute $class->someCall()
There is a way, but for php 5.4 and above...
class MyClass {
function someCall() {
echo "yay";
}
}
$obj = new Myclass();
$ref = array($obj, 'someCall');
$ref();
Hm.. actually it works for static too, just use the reference by name..
class MyClass {
static function someCall2() {
echo "yay2";
}
}
$ref = array('MyClass', 'someCall2');
$ref();
And for nonstatic this notation works as well. It creates a temporary instance of the class. So, this is what you need, only you need php 5.4 and above )
The PHP 5.4 solution above is good. If you need PHP 5.3, I don't think you can do much better than the anonymous function approach, but you could wrap that into a function that acts very similar to the PHP 5.4 method:
function buildCallable($obj, $function)
{
return function () use ($obj, $function) {
$args = func_get_args();
return call_user_func_array(array($obj, $function), $args);
};
}
//example
class MyClass
{
public function add($x, $y)
{
return $x + $y;
}
public static function multiply($x, $y)
{
return $x * $y;
}
}
//non-static methods
$callable = buildCallable(new MyClass(), 'add');
echo $callable(32, 10);
//static methods
$callable = buildCallable('MyClass', 'multiply');
echo $callable(21, 2);
This should work for any number of arguments to any (publicly visible) method.

What's the benefite of binding functions to variables in php 5.3 [duplicate]

This question already has answers here:
Why and how do you use anonymous functions in PHP?
(6 answers)
Closed 9 years ago.
$test = function(){};
It's a new feature of php ver 5.3. I'm interested to know what's the reason.
This is called variable functions in php. We can define some functions and can assign it into variables.This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth.
<?php
function foo() {
echo "In foo()<br />\n";
}
function bar($arg = '')
{
echo "In bar(); argument was '$arg'.<br />\n";
}
// This is a wrapper function around echo
function echoit($string)
{
echo $string;
}
$func = 'foo';
$func(); // This calls foo()
$func = 'bar';
$func('test'); // This calls bar()
$func = 'echoit';
$func('test'); // This calls echoit()
?>
Source
Check these too

PHP closure function appended to stdObject and chained [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Calling closure assigned to object property directly
If I have a class like this:
class test{
function one(){
$this->two()->func(); //Since $test is returned, why can I not call func()?
}
function two(){
$test = (object) array();
$test->func = function(){
echo 'Does this work?';
};
return $test;
}
}
$new = new test;
$new->one(); //Expecting 'Does this work?'
So my question is, when I call function two from function one, function two returns the $test variable which has a closure function of func() attached to it. Why can I not call that as a chained method?
Edit
I just remembered that this can also be done by using $this->func->__invoke() for anyone that needs that.
Because this is currently a limitation of PHP. What you are doing is logical and should be possible. In fact, you can work around the limitation by writing:
function one(){
call_user_func($this->two()->func);
}
or
function one(){
$f = $this->two()->func;
$f();
}
Stupid, I know.

Retrieving the name of the current function in PHP [duplicate]

This question already has answers here:
How do I get the function name inside a function in PHP?
(4 answers)
Closed 8 years ago.
Is there a function that can return the name of the current function a program is executing?
Yes, you can get the function's name with the magic constant __FUNCTION__
class foo
{
function print_func()
{
echo __FUNCTION__;
}
function print_method()
{
echo __METHOD__;
}
}
$obj = new foo();
$obj->print_func(); // Returns: print_func
$obj->print_method(); // Returns: foo::print_method
Maybe via debug_backtrace http://www.php.net/manual/en/function.debug-backtrace.php

Categories