Is it possible to easily "symbolize" a function in PHP - or a name alias for something else?

For example, I hate typing things like:

$x = mysql_escape_string(stripslashes($_GET['x'])); 

Is there a way to smooth out these two functions in init.php or something without writing a separate function that takes a single argument and returns that argument with the functions that apply to it?

My C / C ++ is not very good, but I think it is like #typedef, but for functions?

+3
source share
8 answers

This should do the trick:

 // Macros $mes = "mysql_escape_string"; $ss = "stripslashes"; // Using your macros $x = $mes($ss($_GET['x'])); 

Although I do not recommend coding at all.

I am simply answering this question since you said that you do not want to create any new function and get functionality other than #define.

+2
source

I hope your example is not representative of your project.

  • stripslashes() is optional - if you find it, disable magic_quotes_gpc in php.ini.
  • You should use mysql_real_escape_string() (or the prepare / execute pair) instead of mysql_escape_string() , and there should be a place where your SQL is going to, and not where you extract the values ​​from the URL.

Fix these two problems and your code will degenerate to

 $x = $_GET['x']; 
+8
source
 function myget($string) { return mysql_real_escape_string(stripslashes($_GET[$string])); } 

This is a solution in PHP.

+4
source

I would go with the proposal of Bill Karvin, but I would like to add a perspective that seems important to me.

If you want to replace repeated calls with (b (...)) with c (...), then the likelihood that c has any meaning in itself is outside of a simple composition. Think about the context and what you would call such a function. Often he will have his own semantics, which does not depend specifically on a, b - on one implementation. If so, then it is easy to see it as a (simple) function, which itself is implemented by calling b, then a. Simplicity in this case may provoke you to a "pseudonym" of the function, but in the general case this is probably not very useful.

Now in this particular case, I would think:

  • What is a reasonable name for this compound function?
  • Why is this no longer?
  • Why am I not using query parameters?

Really. That is why you should use query parameters to reduce the risk of SQL injection attacks. MUCHs are more reliable than what you are trying to do here.

+2
source

You can make an anonymous function with create_function :

 $newFunc = create_function('', 'return mysql_escape_string(stripslashes($_GET[\'x\']));'); $newFunc(); 
+1
source

If you need a 1: 1 function mapping, this works, it makes backtracking a bit unpleasant, but it works

 class DB { public static function escape() { $args = func_get_args(); return call_user_func_array('mysql_real_escape_string', $args ); } } DB::escape( $foo ); 

Now I used the func_get_args / call trick here for only one reason:

This notation should work with any function .

However, it would be more optimal to simply link it directly

 class DB { public static function escape($string) { return mysql_real_escape_string( $string ); } } 

And there is no good reason to delete slashes unless you have this terrible "function" in php that activates input with an automatic slash.

 class DB { public static function un_gpc($string) { if( get_magic_quotes_gpc() === 1 ) { return stripslashes( $string ); } return $string; } public static function escape($string, $quote=false) { if( !$quote ) { return mysql_real_escape_string( $string ); } return '"' . self::escape( $string ) . '"'; } public static function escape_gpc( $string , $quote = false ) { return self::escape( self::un_gpc( $string ), $quote); } public static function get( $string , $quote = true ) { return self::escape_gpc( $_GET[$string] , $quote ); } } # Handy Dandy. $q = 'SELECT * FROM FOO WHERE BAR = ' . DB::get( 'bar' ) ; 
0
source

I assume that you want something like a C macro, which is easy to use as a function, but does not have the same overhead as calling a real function. PHP has no such function. Are you trying to make your code faster or more efficient?

0
source

easy, right? elegant, no.

-one
source

All Articles