adding default parameters of the function as result of other function - php

Hi I have question it is possible to adding default parameters of the function as result of other function? For example:
static function addParameter(){
return rand(10,100)
}
function doSomething($year=$this->addParameter()) or
function doSomething($year=class::addParameter())
I need to pass actual year and month to my function. When i pass
function($month=3, $year=2016)
it work then but i not want to write this from hand but want to use function or something to always return actual month and year.

Short answer: no, you can't.
http://php.net/manual/en/functions.arguments.php states that:
The default value must be a constant expression, not (for example) a variable, a class member or a function call.
You can use default null value (if normally function does not take one) and check if parameter is null, then get value from function:
function testfun($testparam = null) {
if ($testparam == null) $testparam = myclass::funToReturnParam();
// Rest of function body
}

Yo can implement it like this
<?php
class Core {
static function addParameter(){
return rand(10,100);
}
}
$core = new Core();
function doSomething($rand){
echo 'this is rand '.$rand;
}
doSomething(Core::addParameter());
You can change the function defination according to your requirement.
hope it helps :)

From the PHP Manual (emphasis mine):
The default value must be a constant expression, not (for example) a variable, a class member or a function call.
So only scalar types(boolean, int, string etc), arrays and null are allowed.
A workaround could be to check if the parameter is null and set it within the function:
function doSomething($year = null) {
if (!$year) {
$year = addParameter();
}
//actually do something
}

Related

Is it possible to Delete a value passed to a method, from within the method

Say I have a method (which in this particular case is a static method), and this method works on a given value. Once completed is there a way that I can automatically in the code, delete the variable (rather than the function copy).
I suspect from all I've read that this is not possible, but there are no clear declarations of such that my searching has found.
An example case:
Static Method:
public static function checkKey($keyValue = null)
{
if(!is_null($keyValue) && !empty($keyValue)) {
if($_SESSION['keyValue'] == $keyValue) {
unset($keyValue,$_SESSION['keyValue']);
return true;
}
unset($keyValue,$_SESSION['keyValue']);
return false;
}
return false;
}
Usage:
$valueToBeChecked = "I want this value unset from within the function"
//PHP page code
AbstractClass::checkKey($valueToBeChecked);
Is there a way that the method checkKey above can delete the value of $valueToBeChecked from within the method checkKey?
The fact it's a static method shouldn't be too critical, it's more the shape of is there a way that the function can delete a value that is set outside the funtion/method, when passed the variable as a parameter?
I realise this is possible if the whole thing is wrapped in a Class and the variable is saved as a class level variable (unset($this->var)), but I'm curious if there's any ability to "reach" variables from outside the scope such as
public static function checkKey($keyValue = null)
{
unset(\$keyValue);
}
I only have limited experience with namespacing but that's my best guess as to if this is possible, how to go about it.
simplified equiviliant outcome:
What I'm trying to reach is this action, entirely within the method:
$valueToBeChecked = "something"
AbstractClass::checkKey($valueToBeChecked);
unset($valueToBeChecked);
You cannot unset a variable from within a function and have that effect propagate. Per the manual:
If a variable that is PASSED BY REFERENCE is unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.
However, you can get equivalent behavior through pass-by-reference and setting to null:
function kill(&$value) {
$value = null;
}
var_dump($x); // NULL
$x = 'foo';
var_dump($x); // 'foo'
kill($x);
var_dump($x); // NULL
This works because, in PHP, there's no distinction made between a symbol that doesn't exist and a symbol that exists with a NULL value.

Passing function as an argument of another function in PHP

I want to pass function (not the result) as an argument of another function. Let's say I have
function double_fn($fn)
and i want to run given function ($fn) two times. Is there such mechanism in PHP? I know you in python function is a kind of variable but is PHP similar?
#edit
is there similar way to use methods?
function double_fn(parrot::sing())
Since 5.3 (note) you do this with closures quite naturally:
function double_fn(callable $fn)
{
$fn();
$fn();
}
double_fn(function() {
echo "hi";
});
(note) type hint only since 5.4
The callable type can be a few things:
a string comprising the function name to call,
a closure (as above)
an array comprising class (or instance) and method to call
Examples of the above is explained in the manual.
Update
edit is there similar way to use methods?
function double_fn(parrot::sing())
Doing that will pass the result of parrot::sing() into the function; to pass a "reference" to the static method you would need to use either:
double_fn('parrot::sing');
double_fn(['parrot', 'sing']);
It's... a bit different. Pass the function name as a string.
function foo()
{
echo "foobar\n";
}
function double_fn($fn)
{
$fn();
$fn();
}
double_fn('foo');
You can use anonymous functions introduced in PHP 5.3.0:
function double_fn($fn) {
$fn();
}
double_fn(function(){
// fonction body
});
or:
$myFn = function(){
// fonction body
};
double_fn($myFn);

How to make function which can handle with diffetent number of parameters with php?

I want to edit user's information. User have a attributes:
User:
name
password
enabled
group
It's fine if i edit all attributes at once - i just get 4 parameters from POST and use function for save changes:
function editUser($oldname,$newname,$password,$enabled,$group){
//**some code**//
$mgroup->getMember()->setName($newname);
$mgroup->getMember()->setPassword($password);
$mgroup->getMember()->setEnabled($enabled);
$mgroup->getMember()->setGroup($group);
//**some code**//
}
But if i am editing just a one or two parameters i cant use my function.
So how can i change function depending on the number of parameters?
For example i edit just pass and enabled attributes, for this my function gonna do:
function editUser($oldname,$password,$enabled){
//**some code**//
$mgroup->getMember()->setPassword($password);
$mgroup->getMember()->setEnabled($enabled);
//**some code**//
}
It's possible to do?
In such cases you usually just pass NULL as argument for the fields where you don't change anything and verify in your setter function:
if ($val !== NULL) {
$this->property = $val; // set it!
}
If you have really a lot of arguments; I'd pass an array to improve readability:
editUser(array('oldname' => …, 'newname' => …, …));
And change the function to:
function editUser ($array) {
if (isset($array['newname']))
$mgroup->getMember()->setName($array['newname']);
// …
}
Try with
function editUser($oldname,$newname,$password,$enabled = true,$group = DEFAULT_GRP_ID)
You should be abled to call editUser with 3, 4 or 5 parameters. PHP isn't like java where you need to redeclare methods.
D.
You can do it like so:
function editUser($oldname,$newname,$password,$enabled,$group){
if($newname != null){
$mgroup->getMember()->setName($newname);
}
//rinse and repeat for all others
}
Then when you call the function pass null for values you don't want to change.
editUser("oldName", "newName", null, null, "group");
why don't you use associated array like array('oldname'=>$oldname,.....) then use value which is not null
Can you define default values for each variable in your function?
function editUser($oldname='',$newname='',$password='',$enabled='',$group='')
{
if !empty($oldname){do something}
....
}
A simple solution to this is to give your arguments default values, check if they're null inside the function and only deal with them if they're not.
function editUser($oldname, $password, $enabled = null, $newname = null, etc.){
//**some code**//
$mgroup->getMember()->setName($newname);
$mgroup->getMember()->setPassword($password);
if ($newname != null)
$mgroup->getMember()->setEnabled($enabled);
if ($group != null)
$mgroup->getMember()->setGroup($group);
//**some code**//
}
One drawback with this method is that you cannot leave $enabled = null implicitly and still set $newname = something explicitly (i.e. you need to call editUser($oldname, $password, null, $newname, etc.), which is both hard to read and maintain when the number of arguments grow. A solution to this is to use named variables. Since PHP does not directly supported named variables, the manual suggests passing an associative array as argument:
function editUser($args) {
$member = $mgroup->getMember();
$member->setName($args["newname"]);
$member->setPassword($args["password"]);
if (isset($args["enabled"]))
$member->setEnabled($args["enabled"]);
...
}

What is the proper way to set default method parameter in PHP?

Let's say you have a method that expects a numerical value as an argument. What's the proper way to set a default value in the event the argument doesn't exist?
public function saveme($myvar = false) {}
public function saveme($myvar = '') {}
public function saveme($myvar = NULL) {}
public function saveme($myvar = 0) {}
All of those are valid, and neither one is "more correct" for all cases. It depends entirely on what the valid range of values are for the function and what the function is supposed to do. Typically, the default type will match the expected input type, unless you want a place-holder for "no value" in which case null is typically used.
If your function can perform something useful with default of 0, use 0. If your function needs to tell the difference between having been explicitly given a single argument of 0 and having been given no arguments, or if 0 is not a sane default, use null.
From the PHP manual:
function myFunc ($myType = 0) {}

How to call a function from a string stored in a variable?

I need to be able to call a function, but the function name is stored in a variable, is this possible? e.g:
function foo ()
{
//code here
}
function bar ()
{
//code here
}
$functionName = "foo";
// I need to call the function based on what is $functionName
$functionName() or call_user_func($functionName)
My favorite version is the inline version:
${"variableName"} = 12;
$className->{"propertyName"};
$className->{"methodName"}();
StaticClass::${"propertyName"};
StaticClass::{"methodName"}();
You can place variables or expressions inside the brackets too!
Solution: Use PHP7
Note: For a summarized version, see TL;DR at the end of the answer.
Old Methods
Update: One of the old methods explained here has been removed. Refer to other answers for explanation on other methods, it isn't covered here. By the way, if this answer doesn't help you, you should return upgrading your stuff. PHP 5.6 support has ended in January 2019 (now even PHP 7.2 and 7.3 are not being supported). See supported versions for more information.
As others mentioned, in PHP5 (and also in newer versions like PHP7) we could use variables as function names, use call_user_func() and call_user_func_array(), etc.
New Methods
As of PHP7, there are new ways introduced:
Note: Everything inside <something> brackets means one or more expressions to form something, e.g. <function_name> means expressions forming a function name.
Dynamic Function Call: Function Name On-the-fly
We can form a function name inside parentheses in just one go:
(<function_name>)(arguments);
For example:
function something(): string
{
return "something";
}
$bar = "some_thing";
(str_replace("_", "", $bar))(); // something
// Possible, too; but generally, not recommended, because makes your
// code more complicated
(str_replace("_", "", $bar))()();
Note: Although removing the parentheses around str_replace() is not an error, putting parentheses makes code more readable. However, you cannot do that sometimes, e.g. while using . operator. To be consistent, I recommend you to put the parentheses always.
Dynamic Function Call: Callable Property
A useful example would be in the context of objects: If you have stored a callable in a property, you have to call it this way:
($object->{<property_name>})();
As a simple example:
// Suppose we're in a class method context
($this->eventHandler)();
Obviously, calling it as $this->eventHandler() is plain wrong: By that you mean calling a method named eventHandler.
Dynamic Method Call: Method Name On-the-fly
Just like dynamic function calls, we can do the same way with method calls, surrounded by curly braces instead of parentheses (for extra forms, navigate to TL;DR section):
$object->{<method_name>}(arguments);
$object::{<method_name>}(arguments);
See it in an example:
class Foo
{
public function another(): string
{
return "something";
}
}
$bar = "another thing";
(new Something())->{explode(" ", $bar)[0]}(); // something
Dynamic Method Call: The Array Syntax
A more elegant way added in PHP7 is the following:
[<object>, <method_name>](arguments);
[<class_name>, <method_name>](arguments); // Static calls only
As an example:
class Foo
{
public function nonStaticCall()
{
echo "Non-static call";
}
public static function staticCall()
{
echo "Static call";
}
}
$x = new X();
[$x, "non" . "StaticCall"](); // Non-static call
[$x, "static" . "Call"](); // Static call
Note: The benefit of using this method over the previous one is that, you don't care about the call type (i.e. whether it's static or not).
Note: If you care about performance (and micro-optimizations), don't use this method. As I tested, this method is really slower than other methods (more than 10 times).
Extra Example: Using Anonymous Classes
Making things a bit complicated, you could use a combination of anonymous classes and the features above:
$bar = "SomeThing";
echo (new class {
public function something()
{
return 512;
}
})->{strtolower($bar)}(); // 512
TL;DR (Conclusion)
Generally, in PHP7, using the following forms are all possible:
// Everything inside `<something>` brackets means one or more expressions
// to form something
// Dynamic function call via function name
(<function_name>)(arguments);
// Dynamic function call on a callable property
($object->{<property_name>})(arguments);
// Dynamic method call on an object
$object->{<method_name>}(arguments);
$object::{<method_name>}(arguments);
// Dynamic method call on a dynamically-generated object
(<object>)->{<method_name>}(arguments);
(<object>)::{<method_name>}(arguments);
// Dynamic method call, statically
ClassName::{<method_name>}(arguments);
(<class_name>)::{<method_name>}(arguments);
// Dynamic method call, array-like (no different between static
// and non-static calls
[<object>, <method_name>](arguments);
// Dynamic method call, array-like, statically
[<class_name>, <method_name>](arguments);
Special thanks to this PHP talk.
Yes, it is possible:
function foo($msg) {
echo $msg."<br />";
}
$var1 = "foo";
$var1("testing 1,2,3");
Source: http://www.onlamp.com/pub/a/php/2001/05/17/php_foundations.html?page=2
As already mentioned, there are a few ways to achieve this with possibly the safest method being call_user_func() or if you must you can also go down the route of $function_name(). It is possible to pass arguments using both of these methods as so
$function_name = 'foobar';
$function_name(arg1, arg2);
call_user_func_array($function_name, array(arg1, arg2));
If the function you are calling belongs to an object you can still use either of these
$object->$function_name(arg1, arg2);
call_user_func_array(array($object, $function_name), array(arg1, arg2));
However if you are going to use the $function_name() method it may be a good idea to test for the existence of the function if the name is in any way dynamic
if(method_exists($object, $function_name))
{
$object->$function_name(arg1, arg2);
}
A few years late, but this is the best manner now imho:
$x = (new ReflectionFunction("foo"))->getClosure();
$x();
In case someone else is brought here by google because they were trying to use a variable for a method within a class, the below is a code sample which will actually work. None of the above worked for my situation. The key difference is the & in the declaration of $c = & new... and &$c being passed in call_user_func.
My specific case is when implementing someone's code having to do with colors and two member methods lighten() and darken() from the csscolor.php class. For whatever reason, I wanted to have the same code be able to call lighten or darken rather than select it out with logic. This may be the result of my stubbornness to not just use if-else or to change the code calling this method.
$lightdark="lighten"; // or optionally can be darken
$color="fcc"; // a hex color
$percent=0.15;
include_once("csscolor.php");
$c = & new CSS_Color($color);
$rtn=call_user_func( array(&$c,$lightdark),$color,$percent);
Note that trying anything with $c->{...} didn't work. Upon perusing the reader-contributed content at the bottom of php.net's page on call_user_func, I was able to piece together the above. Also, note that $params as an array didn't work for me:
// This doesn't work:
$params=Array($color,$percent);
$rtn=call_user_func( array(&$c,$lightdark),$params);
This above attempt would give a warning about the method expecting a 2nd argument (percent).
For the sake of completeness, you can also use eval():
$functionName = "foo()";
eval($functionName);
However, call_user_func() is the proper way.
Dynamic function names and namespaces
Just to add a point about dynamic function names when using namespaces.
If you're using namespaces, the following won't work except if your function is in the global namespace:
namespace greetings;
function hello()
{
// do something
}
$myvar = "hello";
$myvar(); // interpreted as "\hello();"
What to do?
You have to use call_user_func() instead:
// if hello() is in the current namespace
call_user_func(__NAMESPACE__.'\\'.$myvar);
// if hello() is in another namespace
call_user_func('mynamespace\\'.$myvar);
Complementing the answer of #Chris K if you want to call an object's method, you can call it using a single variable with the help of a closure:
function get_method($object, $method){
return function() use($object, $method){
$args = func_get_args();
return call_user_func_array(array($object, $method), $args);
};
}
class test{
function echo_this($text){
echo $text;
}
}
$test = new test();
$echo = get_method($test, 'echo_this');
$echo('Hello'); //Output is "Hello"
I posted another example here
Use the call_user_func function.
What I learnt from this question and the answers. Thanks all!
Let say I have these variables and functions:
$functionName1 = "sayHello";
$functionName2 = "sayHelloTo";
$functionName3 = "saySomethingTo";
$friend = "John";
$datas = array(
"something"=>"how are you?",
"to"=>"Sarah"
);
function sayHello()
{
echo "Hello!";
}
function sayHelloTo($to)
{
echo "Dear $to, hello!";
}
function saySomethingTo($something, $to)
{
echo "Dear $to, $something";
}
To call function without arguments
// Calling sayHello()
call_user_func($functionName1);
Hello!
To call function with 1 argument
// Calling sayHelloTo("John")
call_user_func($functionName2, $friend);
Dear John, hello!
To call function with 1 or more arguments
This will be useful if you are dynamically calling your functions and each function have different number of arguments. This is my case that I have been looking for (and solved). call_user_func_array is the key
// You can add your arguments
// 1. statically by hard-code,
$arguments[0] = "how are you?"; // my $something
$arguments[1] = "Sarah"; // my $to
// 2. OR dynamically using foreach
$arguments = NULL;
foreach($datas as $data)
{
$arguments[] = $data;
}
// Calling saySomethingTo("how are you?", "Sarah")
call_user_func_array($functionName3, $arguments);
Dear Sarah, how are you?
Yay bye!
If you were in a object context trying to call a function dynamically please try something like this code bellow:
$this->{$variable}();
The easiest way to call a function safely using the name stored in a variable is,
//I want to call method deploy that is stored in functionname
$functionname = 'deploy';
$retVal = {$functionname}('parameters');
I have used like below to create migration tables in Laravel dynamically,
foreach(App\Test::$columns as $name => $column){
$table->{$column[0]}($name);
}
Following code can help to write dynamic function in PHP.
now the function name can be dynamically change by variable '$current_page'.
$current_page = 'home_page';
$function = #${$current_page . '_page_versions'};
$function = function() {
echo 'current page';
};
$function();
Considering some of the excellent answers given here, sometimes you need to be precise.
For example.
if a function has a return value eg (boolean,array,string,int,float
e.t.c).
if the function has no return value check
if the function exists
Let's look at its credit to some of the answers given.
Class Cars{
function carMake(){
return 'Toyota';
}
function carMakeYear(){
return 2020;
}
function estimatedPriceInDollar{
return 1500.89;
}
function colorList(){
return array("Black","Gold","Silver","Blue");
}
function carUsage(){
return array("Private","Commercial","Government");
}
function getCar(){
echo "Toyota Venza 2020 model private estimated price is 1500 USD";
}
}
We want to check if method exists and call it dynamically.
$method = "color List";
$class = new Cars();
//If the function have return value;
$arrayColor = method_exists($class, str_replace(' ', "", $method)) ? call_user_func(array($this, $obj)) : [];
//If the function have no return value e.g echo,die,print e.t.c
$method = "get Car";
if(method_exists($class, str_replace(' ', "", $method))){
call_user_func(array($class, $method))
}
Thanks
One unconventional approach, that came to my mind is, unless you are generating the whole code through some super ultra autonomous AI which writes itself, there are high chances that the functions which you want to "dynamically" call, are already defined in your code base. So why not just check for the string and do the infamous ifelse dance to summon the ...you get my point.
eg.
if($functionName == 'foo'){
foo();
} else if($functionName == 'bar'){
bar();
}
Even switch-case can be used if you don't like the bland taste of ifelse ladder.
I understand that there are cases where the "dynamically calling the function" would be an absolute necessity (Like some recursive logic which modifies itself). But most of the everyday trivial use-cases can just be dodged.
It weeds out a lot of uncertainty from your application, while giving you a chance to execute a fallback function if the string doesn't match any of the available functions' definition. IMHO.
I dont know why u have to use that, doesnt sound so good to me at all, but if there are only a small amount of functions, you could use a if/elseif construct.
I dont know if a direct solution is possible.
something like
$foo = "bar";
$test = "foo";
echo $$test;
should return bar, you can try around but i dont think this will work for functions

Categories