PHP 10.0 Blog

What if…

Archive for October 16th, 2006

Inline evals

Posted by Stas on October 16, 2006

Eval function in PHP allows to dynamically create code, which is neat (for people that like such things ;)

<?php
$classname
= “foo”;
eval(
“\$foo = $classname::bar;”);
echo
$foo;
?>

However, there’s a problem with that – it’s slow. It means each time one has to compile code anew and then execute it anew. Slowwww. There’s another method – create_function() – which allows to do like this:

<?php
$newfunc
= create_function(‘$a,$b’, ‘return “ln($a) + ln($b) = ” . log($a * $b);’);
echo
“New anonymous function: $newfunc\n”;
echo
$newfunc(2, M_E) . “\n”;
?>

But the problem would be that it creates a new function – meaning, you can not share symbol table with this code, and you need brand new execution context each time you call it, which is slow too. So, won’t it be nice if you could combine best of the two functions and have code that can be compiled once, but run a number of times while sharing symbol table with the calling code? Would work almost like lambda – though not exactly like – it would not bind variables until really executing the code.
But, if we could use both benefits of create_function() as to making function objects and compiling it only once – and of instant same-scope-ness of eval() – it might allow to do some nice things.

Posted in Engine | 7 Comments »

Some initial thoughts plus Q&A

Posted by Stas on October 16, 2006

Why?

I am starting this in order to record my random thoughts and ideas on PHP and what probably would be nice for PHP to have and what would be nice to add to PHP. This is for myself as well as for everybody else. I have a few things in my “wishlist” for PHP and I thought maybe it would be interesting to put them up somewhere.

So, do you plan to do something with these thoughts?

I really don’t know, so far I plan to get as far as publishing them in the blog. Then I’ll see where it goes.

Why PHP 10.0?

Because I don’t necessarily expect any of it to be in PHP 6, 7, 8 and 9 :)

But idea of Foo you posted sounds stupid!

You are free to think so. You are even free to say it in the comments, only please say why do you think so. Maybe it is stupid indeed and you would make me to See The Light (TM).

But you can’t do this Foo with PHP!

Sure I can’t. That’s why it’s interesting to think about what if I could.

But you can do Foo easily with current PHP!

Please tell me about it in comments. Maybe I missed something.

I like this idea and want to implement it, do I need to ask for a permission?

Not from me. Maybe you should ask PHP community if you want it to be part of official PHP.

Who are you?

My name is Stanislav Malyshev, I wrote some code inside PHP and I work for Zend.

Posted in Metablog | Leave a Comment »