Link to home
Start Free TrialLog in
Avatar of Mark
Mark

asked on

Inline PHP function call

I want to be able to call a PHP function inline .. the only way I know is:

class i { function r($c) { return $c; } } global $i; $i = new i();

$string = "hello {$i->r(myfunction())} world";

Of course I can do $string = "hello ".myfunction().' world' but I prefer a shorter way

Any suggestions?
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Can you step back a little from the technical details and tell us in plain language why you want to do this?  I know of no modern design pattern that rewards compound statements -- to the contrary, they are generally considered antipractices.  It makes sense to package functionality in classes and methods, but compound statements are hard to understand, modify, test and debug, so professionals avoid things like that.  Maybe if we can understand your objectives we can offer a few good ideas.
Avatar of Mark
Mark

ASKER

When performing SQL insert/update statements, I perform string escapes (I have thousands of lines of code not using PDO and not going to upgrade it).
Avatar of Dave Baldwin
I agree with Ray, I would Never do that.  You should keep in mind that PHP 'preprocesses' your code and puts it in a condensed format that will execute properly.  While it won't normally change the order of execution, it will optimize it in ways you don't see.  Write code you can easily understand.  Let PHP take care of optimizing it.
Avatar of Mark

ASKER

I don't want compound statements either but I don't want my code looking like

"INSERT INTO x SET x='".escape($a)."'.....
Avatar of Mark

ASKER

I prefer "INSERT INTO x SET x='{$f->escape($a)}'"; ...

I guess what I'm asking is if I can call the escape function like
"INSERT INTO x SET x='{escape($a)}'"
I don't see any difference between them and I would still not do that.  I always break it into two lines.  First line creates the variable value and the second line uses it.  Let PHP optimize it.

Besides, why aren't you using the mysql_real_escape functions that are recommended?
if I can call the escape function like
"INSERT INTO x SET x='{escape($a)}'"
PHP syntax is not really well constructed for that.  You might consider using arrays of key=>value pairs, where the key is the column name and the value is the information you want to inject into the column.

You might consider refactoring the application.  It would  be easier than trying to get all those queries right one-at-a-time.  A database abstraction layer (which is what PDO gives you) can be a very helpful thing.  A design that uses the active record pattern, like Symfony or Laravel, makes database interaction very easy, without introducing complicated syntax.

Some of this article might be helpful:
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
Avatar of Mark

ASKER

My user defined function escape calls mysql_real_escape_string ...

I appreciate the advice but it's just not practical at the moment .. is there anyway to call a function within a quoted string ...
is there anyway to call a function within a quoted string ...
Not that I know of, at least not in modern PHP, and that kind of coding is discouraged by all the modern designs.  PHP used to allow this for some things, but it's been removed from the language.  An abstraction layer is a better idea.  You can extend the PDO or MySQLi classes to produce your own abstraction layer, making one that is easier to adapt for your existing code.

Might be worth looking into E-E Gigs for some in-depth hands-on help.
My own code looks exactly like this.  On hundreds of pages.
$username2 = $link->real_escape_string($username);
$updsql = "INSERT INTO `users` (`username`) VALUES ('$username2');";
$updrslt = $link->query($updsql);

Open in new window

Avatar of Mark

ASKER

This seems to work ...

function foo($p) { return $p; }

$_ = function($param) { return $param; };

echo "Does this appear? {$_(foo('yes'))}.";

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mark

ASKER

Thank you