📜 ⬆️ ⬇️

Variable variables Properties of properties. Initialization of data from superglobal arrays is easy and simple.


There is an interesting idea of ​​implementing some processor of superglobal arrays, using the example of the $ _REQUEST superglobal array.
A simple way to obtain, process, secure for the data obtained from the form and TP. looks like :

if (!isset($_REQUEST['var'])) $var = NULL; else $var = $_REQUEST['var']; //       

Well, of course, every self-respecting framework will make it more beautiful, for example, for example a separate static class method that can be used anywhere in the project, it is convenient to get the value we need by name:

  class Request { public static function GetVarRequest( $var ){ if (!isset($_REQUEST[$var])) $var = NULL; else $var = $_REQUEST['var']; //       return $var; } } $var = Request::GetVarRequest('var'); 

And finally we come to the point. There is such a thing in PHP as " Variable Variables " (symbolic links), which encouraged me to make this decision. Copy-paste description and examples makes no sense who is not familiar here .
')
How is this related to the above? Maybe I came up with a bike, but still. Immediately bring a piece of working solutions:

  //  ,      $_REQUEST[ 'var1' ] = 'VAR1'; $_REQUEST[ 'var2' ] = 'VAR2'; $_REQUEST[ 'var3' ] = 'VAR3'; echo '<pre>'; print_r( $_REQUEST ); echo '</pre>'; $r = $_REQUEST; $k = array_keys( $r ); $c = count( $r ); for ( $i = 0; $i < $c; $i++ ){ $v = $k[ $i ]; $$v = $r[ $k[ $i ] ]; //       } echo '<br />var1 - '.$var1; // : "var1 - VAR1" echo '<br />var2 - '.$var2; // : "var2 - VAR2" echo '<br />var3 - '.$var3; // : "var3 - VAR3" 

Moreover, this can be done with class properties:

  class VarsVar { //     } $obj = new VarsVar(); $_REQUEST[ 'var1' ] = 'VAR1'; $_REQUEST[ 'var2' ] = 'VAR2'; $_REQUEST[ 'var3' ] = 'VAR3'; echo '<pre>';print_r($_REQUEST);echo '</pre>'; $r = $_REQUEST; $k = array_keys( $r ); $c = count( $r ); for ( $i = 0; $i < $c; $i++ ){ $v = $k[ $i ]; $obj->$v = $r[ $k[ $i ] ]; } echo '<br />var1 - '.$obj->var1; // : "var1 - VAR1" echo '<br />var2 - '.$obj->var2; // : "var2 - VAR2" echo '<br />var3 - '.$obj->var3; // : "var3 - VAR3" 

That is, by framing this case in a static function, by transferring to it an object of an instance of a class (not yet 100% tested), it is possible to perform initialization anywhere and anytime:

  class Request { public static function GetVarRequest( $obj ){ $r = $_REQUEST; $k = array_keys( $r ); $c = count( $r ); for ( $i = 0; $i < $c; $i++ ){ $v = $k[ $i ]; $obj->$v = $r[ $k[ $i ] ]; } } } class VarsVar {} $obj = new VarsVar(); Request::GetVarRequest( $obj ); //  PHP 5           ,   . 

So summarize.
Cons :

Pros :


Conclusion: a universal and very simple way to get data from $ _REQUEST without unnecessary crap can be used only at your own peril and risk. If you are sure that the data always reaches 100% and, if OOP is used, the class properties are initialized, then this method is like a flag in your hands =))

This decision has not yet passed the test of battle, since it has only dawned today and is sharing with you. Here are a couple of simple rules:

And of course, crazy crazy from the world of fantasy. If, for example, the PHP core was stitched for example this kind of $$$ var variables, referring to which would not pop up due to the lack of their initialization, but on the contrary, such variables would immediately be created with a NULL value, then the method I proposed would work smoothly .

Source: https://habr.com/ru/post/148868/


All Articles