PHP 10.0 Blog

What if…

Archive for October, 2006

HTML compact

Posted by Stas on October 30, 2006

HTML compacting is one functionality that is missing in PHP. What does it do? Go to any google.com page and inspect the page source. You’d see all the unnecessary whitespace is removed. You would think it’s a small change, but look on some generated pages on other sites - especially one produced by CMSes using some designer templates - which are formatted to be human-readable and contain a bunch of comments, etc. - and you’d see that whitespace can easily double your page size.

It is not really possible, however, to make your HTML designers abandon the use of whitespace or comments. In fact, you won’t do it anyway - such code would be impossible to maintain. But an extension which can produce “compacted” HTML would be very nice. Like the tidy extension, but tidy doesn’t really know to do whitespace compacting, AFAIK. Maybe it can be modified to do that.

Sure, it’s not simple matter - given the amount of broken and outright crappy HTML out there. People actually can write something like <a href”url”> (yes, no = sign!) and it would work! And be sure, they do. And there’s CSS and Javascript.

Posted in Functions | 21 Comments »

faster, better assert

Posted by Stas on October 26, 2006

For now, assert() function is described as debug function and internally is eval() with checks. Continuing the inlining theme (looks like I think a lot about inlining, don’t I) I’d like to have assert which can:

  • Pre-compile the expression inside, i.e. most of the times I don’t really need to recompile the expression, just substitute variables.
  • Can be quickly turned into no-op if I don’t want it - meaning, just function receiving true/false is not enough, since I can’t no-op it quickly.
  • Of course, can be told what to do if assertion fails, just like the old one.

This can allow people to generate quite efficient pre/post-condition code easily, and turn it on and off on request. I.e. I see my app misbehaving, I turn on asserts, I run it, some assert breaks and I get the full trace of what happened even not touching a debugger once. Not that debugger is something bad - but debugger can’t efficiently checks pre/post-conditions either.
Which brings me to another idea of connecting debugger to these asserts - but debugger should be able to connect to errors (IIRC, some debuggers can now - it’s very easy to do from debugger’s point of view anyway) so if failed assert produces an error that should be enough.

Posted in Functions | 1 Comment »

What is PHP, anyway?

Posted by Stas on October 24, 2006

PHP is nearing it’s sixth version and - believe it or not - nobody really knows what PHP language is. Well, I’m a bit overstating, but a lot of people have all kinds of ideas how PHP should do all kinds of things, but where one looks for the final answer? Where do I look up for how should specific thing in PHP work? OK, there’s of course The Manual - and don’t get me wrong, it is very good. But it’s not the final source on every detail of every trick of the language - it is more description than definition. It does not set the formal rules for every case and every aspect, though it provides pretty good explanation on how the language works. In fact, the only definition of PHP language we have now is the source of the Zend Engine. Which is, with all due respect, not a very accessible source. My opinion is that is long overdue to have something like that. Meaning, a formal definition of PHP language. With formal grammar and stuff.

So, who’s going to write that? Don’t you look at me, I’m not a technical writer and English isn’t even my native language. :) Seriously, if anybody wants the thing done it should be PHP Group effort which is officially (as officially as anything can get in the fluid opensource world anyway) supported and endorsed. And of course, the standard word should be final - meaning, once it is there and manual or engine disagree - that’s manual’s or engine’s fault. Be sure - it is a lot of work. It’s not just rewriting the manual in more formal style - there are bits of the language that 99.9% of developers really didn’t ever think about - like what happens with reference-assignments inside arrays when array is copied? How __get is supposed to work in case when you access unexisting property and then want to modify it? and so on. Describing all these formally is a challenge by itself. But my opinion is - it can be done and it should be done.

P.S. PHP is still under development - and may forever be, and it’s a good thing. So the formal spec would not be frozen - it probably would be amended numerous times. But even the process of amending would make potential proposers to refine their thoughts and think about various aspects they may have forgotten. Which is additional bonus to having it.

Posted in Engine | 2 Comments »

zval custom info

Posted by Stas on October 22, 2006

I wonder if it might be a good idea to allow zvals (if you don’t know what it is, you might want to read something about it before continuing) carry custom information - e.g. in a form of a pointer. This may allow various custom modules to tag zvals - e.g. security module to have zvals coming from users separated from zvals coming from engine. Or debugger could record where particular variable was created/modified.

Of course, that would need a protocol for coordinating this info between modules - so that two modules could store zval info without stepping on each other’s toes.

Posted in Engine | 3 Comments »

Array functions

Posted by Stas on October 20, 2006

Arrays (aka hashtables) are jack-of-all-trades in PHP - virtually all data storage is an array (except for objects - which internally don’t differ much - but let’s leave them alone). However, all array functions are flat - one-dimensional. What if we had some functions for non-flat array operations? One example - let’s suppose we have an array representation of a table:

<?php
$arr
= array(
array(
1,2,3),
array(
4,5,6),
array(
7,8,9),
);
?>

Now, how do I get the first row? Oh, that’s easy - $arr[0]! Now, how do I get the first column? Oops… No good way to do that. Maybe we’d have a function for that? The function, sadly, doesn’t have a good way for that either, but at least it can do it faster.

Then, of course, there’s a whole world of matrices, etc., but that’d be another story.

Posted in Functions | 4 Comments »

Conditional compilation

Posted by Stas on October 17, 2006

That’s one thing I am really missing in PHP - conditional compilation. Mostly what I need is conditional non-compilation, of course. Let’s see:

<?php
function dosomething($param) {
logEnter(“doSomething”);
// now do something

logLeave(“doSomething”);
}
?>

Nothing unusual in this code, looks just fine - you want to know what happens in your functions, you log and maybe even profile entries and exits. However, there’s one small problem. I want these log functions out in production - they cost performance (function call is not very cheap in PHP) and anything useful I can do with them I could do in staging. Of course, I could just add an if($DEBUG_MODE) into functions and make them do nothing. But function call is still there. I could keep two versions - debug and production - but this would lead to a serious mess very soon. I could also surround each call with if($DEBUG_MODE) - but that would look horrible and would be very irritating to write - and on top of that let’s say I am a performance maniac - I don’t want even single extra if be there if it doesn’t have to be. And remember that since we have bytecode caches, the more we push from run-time to compile-time, the better!

So what I would really like to have is something like #ifdef in C - so that certain code is just ignored by the compiler, once certain conditions are met. I don’t really want full-blown C preprocessor - it allows you to do evil things. But something like small conditional compiling here and there won’t hurt.

BTW: if we’d peek outside PHP world, Java doesn’t have this capability too, but in Java you have the compiler, which can statically optimize the code and throw out the parts that are known not to execute, reaching the same goal, effectively. And it even has provision for that in the language definition. Even though I wouldn’t mind having that in Java too - and some people tend to agree with me and some even have more creative solutions. So, returning to PHP, maybe we would have use for something like that too?

Posted in Engine | 4 Comments »

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 | No Comments »

Starting up

Posted by Stas on October 15, 2006

This would be a blog to record my PHP ideas.

Posted in Metablog | No Comments »