Monday, November 18, 2019

PHP templates

I've always knew PHP started as a templating engine and in its core it still is one. When I tried to figure out an easy way for implementing templates for HTML pages, I remembered this and looked around for such solutions.

I totally forgot PHP allows to mix HTML and PHP so effortlessly:

  1. <?php if (...) { ?>
  2. Hello, world!
  3. <?php } ?>

If you don't want PHP in your template, all you have to do is create some semantic markup, that you can easily replace with PHP code:

  1. {{ IF XXX }}
  2. Hello, world!
  3. {{ END IF }}

Then you generate a PHP script from your markup-flavored HTML template and keep it as cache, that changes only if you change the template. It's much faster, because you don't parse it all the time and you can even take advantage of OPcache.

For generating the PHP script you can use regular expressions, but simple explode works equally well:

  1. $ifs = explode("{{ IF ", $template);
  2. foreach ($ifs as $if) {
  3. $cond = explode("}}", $if, 2)[0];
  4. }

In the code above I separated the entire template by {{ IF and then every element again by }}. Now I have the condition I can evaluate. But I never use eval, not even for hardcoded strings. I either implement some custom condition handling, or keep it as a piece of valid PHP code.

Saturday, April 22, 2017

Type Helpers

Starting with PHP 7, a lot of things in PHP moved towards more mature programming language. But what probably will remain for a long time is function inconsistency, argument-wise.

String functions are often [haystack,needle] or [string,param], whilst array functions are often the other way around - [needle,haystack] or [param,array]. I wrote “often”, because sometimes it's not the case. What a mess!

So I decided to provide common type classes with unified order and pull some functions from Util class, where they didn't belong in the first place. I agree a need for Util classes often means wrong class architecture.

Those classes are QS, QN, QA, QD and QE for string, number, array, date and enum functions/methods. They will be optional and usage will be encouraged for creating custom modules for custom projects. The core won't need them, so if you're familiar with PHP functions, those classes won't add anything into the compiler.

They are just helpers and you may use them only for what PHP is the best in – hack something working together in an hour :-)

In JS Framework there is one extra class QC, helping with components and elements.

Monday, February 2, 2015

PHP performance

I always checked what approach in PHP would be the fastest, especially for functionality in iteration (loop). I used simple script with a timer:

  1. function test1()
  2. {
  3. $t = microtime();
  4. while ($i < 1000) {
  5. // HERE TESTED CODE
  6. ++$i;
  7. }
  8. return microtime() – $t;
  9. }

This way I tried the best approach for zero padding or type casting from string to number.

Saturday, May 24, 2014

Mind switch

Switching from PHP to Java is awesome and painful at the same time. Awesome, because I can see the cleaner code, painful, because a lot of things, that PHP (and C#) allows and I got used to it, is now harder to do and process in my head.

But it also works the other way around. Now I can see some practices in PHP are not as great (or safe) as I thought. I already learned to keep warn level up to E_NOTICE and kill’em all. That means initialize variables. It’s more code, but it’s also more predictable.

This time I turned back to PHP and I can see why the object model used to be so exaggerated – because in compiled langs doesn’t matter how many includes there are. In PHP each include takes time (I/O) and therefore you should use merged/joined/minified version in production, or even better - use OPcache.

If you try to keep the project the same across different platforms, you always should stick with the rule of Java, where each public class has it’s own source code file.