While I've taught myself a great deal, today I have been struggling with an unexpected behaviour of calling parameters in a function.
Here's an example function:
public function example($foo = null, $bar = null) {
if ($foo) {
// Do something
}
if ($bar) {
// Do Something
}
}
Now if I write a call that looks like the following, I was expecting to call $bar (the second parameter) instantly, which is not the case - and it makes sense to me why.
$this->example($bar);
Is there a way to just call the second parameter without using the following?
$this->example(null, $bar);
Well, like was suggested by Darren and his example, you can use function func_get_args, but there isn't much difference, so you should use which one fits you.
Using func_get_args, though doesn't seem too comfortable, so what is the reason not to use direct params? Pick any value you want to use as indicator (null, false, 'barambam') and do something like this:
const ARGFALSE = 29292929299;
function foobar($f, $o, $o, $b, $a, $r){
if (!$f)// equivalent to $f != false
echo 'First arg should not be used.';
If (!$o == ARGFALSE)echo 'Second is taging along with the first';
foobar(false, ARGFALSE);
Still, in some (or most) cases you may do it backwards (another example):
function getUsers($type, $limit = false) { // or your default value. It can be almost anything.
$query = 'Select from users where type = ?';
If($limit){
$query .= ' limit ?';
return $result = Db::run($query, [$type, $limit]);
}else{
return $result = Db::run($query, [$type]);
}
}
Related
Straightforward:
I want to write some code which tells if a variable was passed by reference or not.
For example:
<?php
function isReference($variable) {
//return TRUE if &$something was passed
//return FALSE if $something was passed
}
$text = 'Anything';
$a = isReference(&$text); //return TRUE
$b = isReference($test); //return FALSE
?>
For those who are curious - why do I need it?
Firstly I do not like to leave problems unsolved.
Secondly, I am currently enhancing by skills by writing an extension to mysqli, which would prepare statements similar to how PDO does. If anybody knows the difference between bindParam and bindValue in PDO, they know that it's a workaround of this question. I can just make two functions, but I wonder if it's possible with one.
Thanks in advance.
Here's a way to do it without using the debug_zval_dump function:
function isReference($variable) {
$variable = array($variable);
$arg = func_get_arg(0);
$isRef = isset($arg[0]) && $arg === array($variable[0]);
$variable = $variable[0];
return $isRef;
}
Note: there is only a single case when this will not work:
$text = array(&$text); // although i don't see why such a structure could be used
isReference($text); // will wrongly return false
Obviously you can bypass this limitation by using a random unique key (instead of 0).
You can use debug_zval_dump to dump a string representation of an internal zend value to output:
function isRef(&$val) {
ob_start();
debug_zval_dump(&$val);
$ret = ob_get_clean();
preg_match('~refcount\((\d+)\)~', $ret, $matches);
return $matches[1] - 4;
}
$var1 = 'Var1';
$var2 = &$var1; // passed by ref
if(isRef($var2)) echo 'Passed by ref';
But be aware of PHP - 5.4.
Simply checking for the default value seems to work fine in my tests. Obviously it wont work if $t is already set to 'x' but you could change the default value to something totally unlikely to workaround this:
function test(&$t='x'){
if($t!='x') $t = 2;
}
test(); echo $t; //outputs: null
test($t); echo $t; //outputs: 2
My current way:
class A {
public function function_b($myint) {
if (!is_numeric($myint)) return false;
// code ...
}
}
I would like to abandon the function is_numeric() like this:
public function function_b(Integer $myint) {
// code ...
}
It works with arrays like this:
public function function_c(Array $arr) {
// only executes following code if $arr is an array / instance of Array!
}
Note: the function has to return false if the value isn't a number (int)! I don't want to cast it.
How would you short my current code? Thanks in advance!
You can't force strict types in function prototypes in PHP inherently, because it's not a strictly typed language. PHP is a weakly typed language and trying to go against the grain will only hurt you in many situations. Also, is_numeric does not guarantee that your value is of type int (for what it's worth).
What you can do is analyze your need for why you think this approach is necessary in the first place and decide on how to best implement this without creating potential for bugs.
For example, take the following scenario where what your method expects is an ID for a database query.
class MyClass {
public function getUser($id) {
if (!is_int($id)) {
throw new Exception("Invalid argument supplied. Expecting (int), but argument is of type (" . gettype($id) . ").");
}
// Otherwise continue
$db = new PDO($dsn);
$stmt = $db->prepare("SELECT username FROM users WHERE user_id = ?");
$stmt->execute(array($id));
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $result;
}
}
$MyObject = new MyClass;
$result = $MyObject->getUser($_POST['id']);
/* The problem here is $_POST will always be of type string. */
What this should tell you is that it makes no sense to force type checking here since PHP will have done the right thing for you had you just let it alone.
The question you need to be asking yourself is not "How do I force strict typing?", but rather "Why would I need to force strict typing at all?".
You should look into typecasting:
http://php.net/manual/en/language.types.type-juggling.php#language.types.typecasting
Just use (int) when accessing the value to typecast it to an integer.
You could just typecast it:
public function function_b($myint) {
$myint = (int) $myint;
}
Or better yet add a public setter to class A which will do it for you every time you set the value:
class A
{
public function setMyInt($myInt)
{
$this->myInt = (int) $myInt;
}
}
-- Update (based on comment) --
class A
{
public function doSomethingWithAnArray(array $array)
{
....
}
}
Notice the keyword array in the signature of the doSomethingWithAnArray method, now if you don't pass an array to this function PHP will throw a fatal error and cease code execution. This is known as typehinting, and can be applied to objects as well.
function needsInteger($int) {
if (((int) $int) != $int) return false;
// ...
}
The advantage here is that you can still accept loosely typed parameters, but the non-strict equality check against the cast value will yield an acceptable result.
I have a few "setter" methods across classes, and for convenience I've added an optional parameter $previous, which takes an argument by reference and populates it with the existing value before replacing it with the new one. For example:
public function set_value($key, $value, &$previous = null)
{
$previous = $this->get_value($key);
$this->_values[$key] = $value;
return $this;
}
This works fine; however in some circumstances, the corresponding "getter" method is a bit process intensive, and running it unconditionally is a waste. I figured I could test:
if(null !== $previous)
{
$previous = $this->get_value($key);
}
This doesn't work though, as often the variable passed as the argument for $previous hasn't been previously defined in it's scope, and defaults to null anyway. The only solution I've hacked out is:
public function set_value($key, $value, &$previous = null)
{
$args = func_get_args();
if(isset($args[2])
{
$previous = $this->get_value($key);
}
$this->_values[$key] = $value;
return $this;
}
Or, to one-line it:
if(array_key_exists(2, func_get_args()))
{
// ...
}
I don't like the method body being reliant on the argument indices (when it seems it should be unnecessary) Is there a cleaner way to achieve what I'm after here?
I've tried:
if(isset($previous)){}
if(!empty($previous)){}
if(null !== $previous){}
Neither work.
Possible solutions thus far:
if(func_num_args() == $num_params){}
if(array_key_exists($param_index, func_get_args())){}
// 5.4
if(isset(func_get_args()[$param_index])){}
// 5.4
if(func_num_args() == (new \ReflectionMethod(__CLASS__, __FUNCTION__))
->getNumberOfParameters()){}
#DaveRandom -- So, something in the area of:
define('_NOPARAM', '_NOPARAM' . hash('sha4096', microtime()));
function foo($bar = _NOPARAM)
{
// ...
}
#hoppa -- Use case:
$obj->set_something('some_key', $some_value, $previous) // set
->do_something_that_uses_some_key()
->set_something('some_key', $previous) // and reset
->do_something_that_uses_some_key()
-> ...
Instead of:
$previous = $obj->get_something('some_key'); // get
$obj->set_something('some_key', $some_value) // set
->do_something_that_uses_some_key();
->set_something($previous) // and reset
->do_something_that_uses_some_key();
-> ...
possibly not how you wanted to solve your problem (testing somehow optional arguments), but this is how I would implement it:
public function set_value($key, $value)
{
$this->_values[$key] = $value;
return $this;
}
public function set_get_value($key, $value, &$previous)
{
$previous = $this->get_value($key);
$this->_values[$key] = $value;
return $this;
}
Use case example:
$obj->set_get_something('some_key', $some_value, $previous) // set AND get
->do_something_that_uses_some_key()
->set_something('some_key', $previous) // and reset
->do_something_that_uses_some_key()
-> ...
Why use another function?
This solution has a few advantages:
the name is more explicit, less confusion for other coders
no hidden side effects
solves your problem with (undefined) variables already having a value
no overhead of calling func_num_args, or some other "meta" function
EDIT: typo in code.
EDIT 2: removed default value of &$previous set_get_value() function (thanks to draevor)
Extracted from the comments / discussion above:
In order to check whether the argument was passed you have 2 options - check the value of the argument against a value (as you've done with null) or check the number of arguments.
If you go with the first option, there's no value that cannot be passed from outside the function, so there will always be a chance for false positives (the same thing that's happening now with null). DaveRandom's example with a random string should be enough for most cases though, but I see it as overkill.
I think the second option is the most clean (fast, readable, etc). As a small improvement over what you've already done with func_get_args, I'd use func_num_args - this way you'll be checking the number of passed arguments, not the argument indices.
This is a minor thing, but it's been bugging me for a while. I've wracked my brain for a way to write statements like this without any repetition of code. For example:
echo isset($array[0])? $array[0]: 'not set';
$var = empty($other_var)? '$other_var not set': $other_var;
Is there some sort of test-and-return (for the former) or test-and-set (for the latter) operator I don't know about? This may seem like a minor point, but the duplication seems unnecessary and can lead to very long lines that can complicate maintenance. Consider:
$another_var = array_key_exists($array[Utility::FindIndex($username)][Constants::App_CRITERION], $haystack[NthDimension('my dimensional indicator')])? $array[Utility::FindIndex($username)][Constants::App_CRITERION], $haystack[NthDimension('my dimensional indicator')]: 'not set';
Yes, yes, the above line is totally contrived but it's not unthinkable that something like this could occur. It just seems strange to me that there isn't a way to test something and assign it's value (if true) without repetition without repetition.
It won't handle the isset () case, which is a main use case for this pattern, but PHP 5.3 does have a short form for the ternary operator.
$new_val = $if_true ? $if_true : $if_false;
can be shortened to
$new_val = $if_true ?: $if_false;
I couldn't find it in the docs, strangely, but here's a question about it: What is ?: in PHP 5.3?
I think in PHP 6 there was a function planned called issetor or something similar. But I can't remember the name. And PHP 6 is dead either way.
So, write it yourself:
function issetor(&$var, $default) {
return isset($var) ? $var : $default;
}
echo issetor($_GET['me'], 'you');
If you want to make it even more abstract, look at this:
function isor(&$var, $default, $condition) {
if (!is_callable($condition)) {
throw new InvalidArgumentExpression('condition not callable!');
}
return $condition($var) ? $var : $default;
}
// this is equivalent to issetor($_GET['me'], 'you');
echo isor($_GET['me'], 'you', function(&$var) { return isset($var); });
// but you may use a more complicated thing here, too:
echo isor($_GET['me'], 'you', function($var) use($allowed) { return in_array($var, $allowed); });
// this is equivalent to:
echo in_array($_GET['me'], $allowed) ? $_GET['me'] : 'you';
// now the "normal" version is still shorter. But using isor allows you to store often used $condition closures in variables. For example, if you want to check if several values are in an array, you could write:
$isAllowed = function ($var) use ($allowed) {
return in_array($var, $allowed);
};
$a = isor($a, 'default', $inAllowed);
$b = isor($b, 'default', $inAllowed);
$c = isor($c, 'default', $inAllowed);
$d = isor($d, 'default', $inAllowed);
If you want to pass additional variables to your condition function without always useing closures you may add another argument. (Note I didn't use an argument array and call_user_func_array, because you may not pass per reference using it, but obviously you may extend the code so it does so.)
function isor(&$var, $default, $condition, $addArgument = null) {
if (!is_callable($condition)) {
throw new InvalidArgumentExpression('condition not callable!');
}
return $condition($var, $addArgument) ? $var : $default;
}
// the above in_array condition:
echo isor($a, 'default', 'in_array', $allowed);
I'm sorry for asking this question, but I'm not good in php (beginner).
Could you please explain what $arg means in this piece of code? (it's from one of drupal modules)
function node_add_review_load($arg) {
global $user;
$add_review = FALSE;
$current_node = node_load($arg);
$type =$current_node->type;
$axes_count = db_result(db_query("SELECT COUNT(*) FROM {nodereview_axes} WHERE node_type='%s'", $type));
if (variable_get('nodereview_use_' . $type, 0) && $axes_count) {
$add_review = db_result(db_query("SELECT n.nid FROM {node} n INNER JOIN {nodereview} nr ON n.nid=nr.nid WHERE uid=%d AND reviewed_nid=%d", $user->uid, $arg));
}
return $add_review ? FALSE : $arg;
}
Thank you.
http://nl.php.net/manual/en/functions.arguments.php
When a programmer uses node_add_review_load() he can pass the argument which can be used in the function.
The function returns another value if the argument $arg is different.
So the programmer can do this:
node_add_review_load("my argument");
//and the php automatically does:
$arg = "my argument";
//before executing the rest of the function.
In general, arg is short for "argument," as in "an argument to a function." It's a generic, and thus unhelpful, name. If you'd just given the method signature (function node_add_review_load($arg)) we'd have no idea.
Fortunately, with the complete function body, we can deduce its purpose: it is the node_id. This function loads the node identified by $arg and then tries to find a corresponding row that's loaded, and that the code then tries to find a corresponding review for the current user. If successful, the function will return that same node_id (i.e., $arg); otherwise it will return FALSE.
It's an argument.
Example,
// function
function sum($arg1, $arg2)
{
return $arg1+$arg2;
}
// prints 4
echo sum(2,2);
You don't have to call it $arg for it to be valid. For example,
function sum($sillyWilly, $foxyFox)
{
return $sillyWilly+$foxyFox;
}
And it would work the same. You should give arguments useful names. In this case, the argument $arg is bad programming practice because someone like you would look at it and get confused by what it means exactly. So in cases where you make functions, be sure to use useful names so you'll remember.