So I have this functioin
public static function variable( &$var )
{
if( isset($var) )
{
return $var;
}
return false;
}
I want to give it a variable / array[key] and if the key does not exists it returns false instead of an error.
I am using laravel 4. It works fine as a function and if I create a class Helpers and autoload it from a libraries folder inside the app folder I can access it via Helpers::variable and it works fine.
// those two work fine
<?php
class Helpers {
public static function variable( &$var ){
return isset($var) ? $var : false;
}
}
function variable( &$var ){
return isset($var) ? $var : false;
}
However if I create a package (to use with composer, more functions to come, so I want it in here) https://github.com/lukasoppermann/php-utilities I get an error
$test is undefined
when I use Utilities::variable($test)
. $test of course is undefined, but with the two other functions / method it works as expected and I get false and no error.
Any idea why I is like this? I am using Facades and the class has a namespace, is that a problem?
if you pass a variable to a function, it has to be defined first. There is no way (at least I don't know one) around it. You might wonder why it works if you only use isset($test)
. Thats because isset()
and empty()
are language constructs and not functions.
From kunststube.net/isset:
isset and empty are not actually regular functions but language constructs. That means they're part of the PHP language itself, do not play by the normal rules of functions and can hence get away with not triggering an error for non-existent variables.
And if you want to check if a key exists inside an array, array_key_exists is probably better than isset.
Edit:
Ah I see now. It is indeed a problem with the Facades. As per Gustek's answer, passing a variable by reference, creates the variable (if it is undefined). That's why your first two approaches work fine. However, with your Facades, you don't call your static method directly, but through another layer. And this one receives your argument by value and not by reference.
// See: Illuminate\Support\Facades
public static function __callStatic($method, $args)
{
$instance = static::resolveFacadeInstance(static::getFacadeAccessor());
switch (count($args))
{
case 0:
return $instance->$method();
case 1:
return $instance->$method($args[0]);
case 2:
return $instance->$method($args[0], $args[1]);
case 3:
return $instance->$method($args[0], $args[1], $args[2]);
case 4:
return $instance->$method($args[0], $args[1], $args[2], $args[3]);
default:
return call_user_func_array(array($instance, $method), $args);
}
}
And from php.net:
Note: None of the arguments of these magic methods can be passed by reference.
In PHP when Your try to use variable that was not assigned any value before You get a NOTICE error and variable is created with NULL value (isset returns false for null)
It would be expected to get same error in Your case but when You try to reference undefined variable it gets created without error, seems to be made like that intentionally. You can read about it in documentation(2nd note)
Better question would be why they did it like this.