I have a controller function with 3 argument.Default parameter is set for all of them
When calling this function if i have to give the 3rd parameter only how can i specify i am passing the 3rd parameter
public function manage_class($msg="",$id=0,$class_type="all")
i want to call this function only by the 3rd parameter
I think that if you pass null to the 1st and 2nd parameter, it will grab the default values
like:
manage_class(null,null,3rd_value)
Method would be to first set parameters with possible change of default value, and then strict defaults:
public function manage_class($class_type="all", $msg="", $id=0). Maybe I've got you wrong, or something.
And by the way, if you need to pass only 1 parameter, why you passing other two as arguments?
Well in that case you could define the function manage_class like i said above but then with an array:
manage_class($args = array())
{
//do something with this
echo $args['third'];
}
and then you can cal manage_class with an url like this (sitename/call_manage_class/null/null/foo
call_manage_class($first, $second, $third)
{
if($first == null) {
$first = "";
}
//etc
manage_class(array('first'=>$third
'second'=>$second
'third'=>$third));
}
Related
I'm trying to construct an intermediary function for my WordPress theme that handles all the checking and returning of transients. I want to call the transient function from a template, passing in the transient name and function to call if the transient doesn't exist. Furthermore, I need to pass parameters to use if the function is actually called.
I can't find anything on passing multiple parameters together to use later. For several uses, I can rely on the default parameters of the eventually called functions and so set the third parameter, $args to false. I looked into using serialize, but that's for one variable at a time, and I need to be able to pass several parameters at once.
Where I call the transient function:
$posts = transitize('transientname', 'functionname', "4, false, array($post->projectsiteid)");
The transient-checking function:
function transitize($transientname, $functionname, $args=false) {
if (false === ($transient_exists = get_site_transient($transientname))) {
$functionresult = ($args) ? $functionname($args) : $functionname();
set_site_transient($transientname, $functionresult, HOUR_IN_SECONDS);
return $functionresult;
}
else {
return $transient_exists;
}
}
How can I prepare the third parameter, containing 3 parameters for later use, such that the later function call works?
Change the third parameter to an array:
$posts = transitize('transientname', 'functionname', array(4, false, array($post->projectsiteid)));
Change this line to use call_user_func_array:
$functionresult = ($args) ? call_user_func_array($functionname, $args) : $functionname();
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
}
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"]);
...
}
Bit tricky question but this saves lot of time if you have to modify the script later on to add a third parameter which is a optional, while second was also optional and mostly it was used as second parameter not set, now when you add the third parameter you have to scan through everywhere to find the code snippet and add/set second and then add third ..
is there a easy way to do this ?
example :
public static function result($sql, $i = 0, $r = 0) {
//code
}
if we need to add the third $r, later on and most of the times if the code was used as
result($sql);
now everywhere i had to scan and do
result($sql,0,10);
is there a easy way to set the third parameter without setting the second one ?
No this is not possible, however you may want to consider setting the optional parameters to be null, and then setting the default in the function.
For example:
public static function result($sql, $i = null, $r = null) {
if(is_null($i)){
$i="default";
}
... etc
}
This way, you can maintain the default in the function instead of having to duplicate it throughout your codebase.
add an array of possible parameters
public static function result($sql, $settings=null) {
if(is_array($settings) {
if(isset($settings['i']) {
// condition code
}
if(isset($settings['r']) {
// condition code
}
}
// normal code
}
I'm trying to write a function in a CodeIgniter controller which can take optional parameters. However, I always get Missing Argument warnings. I'm not trying to suppress the warnings - I'm trying to declare the parameters as optional (maybe they could be empty strings if they don't exist, or something).
What is it I'm missing?
Thanks
Mala
public function my_optional_test($not_optional_param, $optional_param = NULL)
{ $this->stuff(); }
have you tried this?
For example, let’s say you have a URI like this:
example.com/index.php/mycontroller/myfunction/hello/world
example.com/index.php/mycontroller/myfunction/hello
Your method will be passed URI segments 3 and 4 (“hello” and “world”):
class MyController extends CI_Controller {
public function myFunction($notOptional, $optional = NULL)
{
echo $notOptional; // will return 'hello'.
echo $optional; // will return 'world' using the 1st URI and 'NULL' using the 2nd.
}
}
Reference: https://codeigniter.com/user_guide/general/controllers.html