I’m working on a project and I have a method called View::import. This receives two arguments string name, reference mixed value (I wrote thus to better understand, but is PHP!).
Currently, to call this method, I need to do:
$test = 1;
View::import('test', $test);
Works very fine, but I like to call too:
View::import('test', 1);
For static cases, that I don’t need replace original variable content, only “store on the fly”.
The full method is:
private static $globals;
public static function import($key, &$value){
self::$globals->{$key} = &$value;
}
If I change to:
public static function import($key, $value){ // no-reference
I can’t change original values, on some cases. And I won’t make a new method like import_static or similar.
Exists someway to overflow this method? (I know that PHP don’t support this perfectly).
The closest code is:
View::import('test', $temporaryTest = 1);
But I think that is a big workarround, not?
You can ‘overflow‘ this by changing the design of your code. For example do something like this:
and then you will be able to write:
as well as:
Below is some alternative solution, that actually involves creating new, but meaningful method:
Applying the above it will be easy for you to see what really happens when you invoke the specific method.
Did it help you?