Seva's notes

Wisdom, Awareness, Responsibility :)

Archive for the ‘web’ Category

Zend Framework View Script Recursion

leave a comment »

We know that in Zend Framework View Scripts it’s possible  to run in a recursion using a View Helper – either Partial, or Action, or a custom one.

However all of these become a certain execution overhead in case we need just a simple one-time tree traversal. Also for readability purpose, it would be nice to have the recursion defined inline.

Luckily, since PHP 5.3 we can use self referencing closures to define the behavior within the script:


<?php
$showTree = function(array $entities) use(&$showTree) {?>
 <?if(!$entities) return?>
 <ul>
 <?foreach($entities as $entity):?>
 <li><?=$entity->getName()?></li>
 <?=$showTree($entity->getChildren())?>
 <?endforeach?>
 </ul>
<?}
?>
<?=$showTree($this->entities)?>

Written by Seva

2013-04-03 (April 3) at 11:37:04

Simple No-Framework Object Oriented Multi-Layer MVC Application Example

with one comment

Recently I’ve created the subj. It’s a memory game, written in pure PHP/HTML, called “PHP Memory”.

What was important for me to demonstrate is the next principles:

  1. Even if you don’t use a framework, you should write modular code with appropriate architecture.
  2. Despite that, the application design and code should match its required functionality with no overhead of unneeded patterns and abstraction or preparations for future enhancements, unless they’re planned.
  3. Nevertheless, the code should be readable and maintainable.
  4. The bottom line is – developing a framework versus developing an application are completely different tasks. The strategy and tactics should vary very much. Of course, developers should learn from the frameworks source code, since they accumulate collective experience of great coders, but the ideas implemented in frameworks sometimes not required or even harmful while developing an application.
The demo and source code can be found here.

Written by Seva

2011-08-30 (August 30) at 01:59:26

cURL HTTP1.1 empty POST bug

with 2 comments

Today we spent almost 2 hours on a weird discrepancy between our development and staging environments. It’s pretty rare, that I experience such low level issue, thus in my opinion it’s worth mentioning here.

Last weeks we were busy developing integration to a new data vendor. Everything went well until we deployed the application to stage.
Suddenly we started receiving HTTP status 411 on one of the calls. Since we work with cURL library, which we believed is stable enough, we thought the problem is somewhere between the source code and environment configuration.

Later we found that the same request gets accepted if sent from a client other than cURL (e.g. chrome-poster). The unique about this request is that it’s sent with POST method (the vendor’s strict requirement) but the content body is empty.

In the end we discovered that newer version (since 7.20) of cURL interprets missing body as a negotiation request – sends Expect: 100-continue header and Content-Length: -1.

So, the immediate solution was to send empty content body (zero-length string) to cURL, which aligned the behavior in all the environments.

On the way we discovered a useful option CURLINFO_HEADER_OUT, which enables a possibility to further retrieve the headers sent by cURL to the remote host. From now we use it in our error handling mechanism to trace the sent headers as well.

What can we conclude from this story?

  1. Try to synchronize the software of all of your environments. If possible, use exactly the same version OS version, libraries, tools, etc. It’s very easy if you host your applications on VPS and use VMs for development and staging servers. The least convenient case is when you have OSs of different architecture and (e.g. Windows for development and Linux for production).
  2. Don’t underestimate importance of error handling. Find the optimal level of handling for your application, which will be easily extended and configured.
Hope my sincere advices will help you, my dear friend, to save your precious time.

Written by Seva

2011-08-03 (August 3) at 12:47:35

Heart-Touching Quotation

leave a comment »

…When I was going to school we were always taught, “In the olden days of computing, computers were expensive and programmers were cheap. Now it’s the reverse. Therefore…” We are back to the future. At internet scale, programmers are (sometimes) cheap compared to the cost of electricity.

Kent Beck

Written by Seva

2010-04-15 (April 15) at 12:06:29

Is Facebook PHP HipHop bullshit?

with 17 comments

Update: with the release of HHVM 2.0 the below becomes entirely obsolete. Cheers!

Yes, it finally happened – Facebook announced the PHP to C++ translator. Hurray!
So, after reading the post I can assume it:

  1. parses PHP code,
  2. extracts PHP AST,
  3. converts to C++ AST and
  4. generates C++ code.

Honestly, it’s not the best way of optimization, but probably is a quick win (BTW, why did it take for them so long?), which theoretically might indeed generate efficient C++ code.

But wait, what do they report? “We’ve reduced the CPU usage on our Web servers on average by about1 fifty percent3, depending on the page2.”

I have some doubts:

  1. Where is the objective and comparative statistics?
  2. Did they even collect the general statistics, or just tested some pages to see the difference?
  3. 50%? That’s it? And how about the fact that C++ is actually 3-500 times faster (also here) (That would be 70% to 99% reduce of CPU). In fact the well known PHP Accelerators provide the same level of performance improvement as HipHop! It’s interesting, did they try any of them before entering the so called “Hackathon”.

In addition to reflections above, looking at the profiles of 2.5 coders which hacked the translator, I sorrowfully tend to estimate that the answer for the question in this Post’s subject is positive. Nevertheless I’m full of sincere hope that further info will dispel my suspects and prove that HipHop is actually a brilliant peace of software.

Written by Seva

2010-02-03 (February 3) at 02:05:49

Posted in development, integration, php, software, thought, web

Tagged with

Download a Google Document with Zend_GData API

with 3 comments

Today I’ve thought of a way of showing a private Google Document on a custom page with no need to authenticate. I was surprised that the issue was not covered in any documentation; I also didn’t succeed to find a quick solution on the Internet. Thus I’m sharing with you the solution using Zend_GData library, my dear friend, with hope to save your valuable time:

$client = Zend_Gdata_ClientLogin::getHttpClient($user, $pass, Zend_Gdata_Docs::AUTH_SERVICE_NAME);
$api = new Zend_Gdata_Docs($client);
$doc = $api->getDocument($docId);
$client->setUri($doc->getContent()->getSrc());
$response = $client->request();
$html = $response->getBody();   

Voila! The $html variable now contains the clean and image/css/js safe content to use (including <HTML> tags)

Note: The code lacks exception handling for simplicity, so please keep that in mind and fulfill upon need.

Written by Seva

2010-02-01 (February 1) at 06:35:51

SyntaxHighlighter

leave a comment »

Shame on me! I’ve discovered that wordpress.com provides syntaxHighlighter functionality just today. So I hurry to report that the only 2 posts in this blog, which contain code snippets have been immediately converted:

<a href="https://sevalapsha.wordpress.com/2008/06/26/copy-nodes-xml-documents-java-dom/">
    Copying nodes between XML documents with Java DOM
</a>
&amp;
<a href="https://sevalapsha.wordpress.com/2008/08/11/prototype-google-analytics-javascript/">
    Prototype styled Google Analytics javascript snippet
</a>

BTW, isn’t it weird I don’t have one snipped in PHP yet?

Written by Seva

2009-09-16 (September 16) at 09:14:52

Posted in html, java, javascript, php, software, web, xml

Tagged with

Moving on

leave a comment »

Today was the 1st day of me working in the new company (yes, my dear friend, I bid farewell to Nielsen Online) – EasyToBook GmbH – as Software Architect. Unfortunately I’ve got no chance to either design any architecture or to develop any software yet, but succeeded to meet nice and talented people and spent some sweet time reading the documentation.

Written by Seva

2009-08-26 (August 26) at 10:21:12

Posted in development, future, php, software, web

Tagged with

PHP Frameworks popularity estimation

with 2 comments

About 2.5 years passed since I posted the development activity comparison of major PHP frameworks. Since then I’ve got about 4.5k hits from search engines, which IMHO is not such a bad sample to summarize people’s interest in each of them.

Again, no conclusions – just pure numbers 🙂

http://tr.im/lnXq:

Query Hits Zend CakePHP Symfony CodeIgniter Prado Seagull eZ
zend framework vs cakephp 573 573 573 0 0 0 0 0
zend vs cakephp 319 319 319 0 0 0 0 0
cakephp symfony 258 0 258 258 0 0 0 0
zend vs symfony 254 254 0 254 0 0 0 0
cakephp vs zend framework 217 217 217 0 0 0 0 0
symfony vs zend 210 210 0 210 0 0 0 0
zend framework vs symfony 187 187 0 187 0 0 0 0
symfony vs cakephp 185 0 185 185 0 0 0 0
zend framework vs 176 176 0 0 0 0 0 0
codeigniter vs zend framework 143 143 0 0 143 0 0 0
prado framework 139 0 0 0 0 139 0 0
cakephp vs zend 139 139 139 0 0 0 0 0
symfony vs zend framework 138 138 0 138 0 0 0 0
zend framework cakephp 131 131 131 0 0 0 0 0
prado vs zend 103 103 0 0 0 103 0 0
zend framework vs codeigniter 84 84 0 0 84 0 0 0
cakephp vs symfony 82 0 82 82 0 0 0 0
prado vs cakephp 81 0 81 0 0 81 0 0
prado vs symfony 80 0 0 80 0 80 0 0
zend vs codeigniter 76 76 0 0 76 0 0 0
zend or cakephp 69 69 69 0 0 0 0 0
codeigniter vs cakephp 68 0 68 0 68 0 0 0
codeigniter vs zend 62 62 0 0 62 0 0 0
zend vs code igniter 60 60 0 0 60 0 0 0
cakephp zend framework 59 59 59 0 0 0 0 0
codeigniter vs symfony 55 0 0 55 55 0 0 0
cakephp symfony zend 52 52 52 52 0 0 0 0
cakephp vs prado 50 0 50 0 0 50 0 0
symfony zend framework 48 48 0 48 0 0 0 0
cakephp vs codeigniter 48 0 48 0 48 0 0 0
zend framework vs code igniter 46 46 0 0 46 0 0 0
zend framework vs. cakephp 46 46 46 0 0 0 0 0
symfony vs codeigniter 45 0 0 45 45 0 0 0
symfony vs prado 39 0 0 39 0 39 0 0
zend vs prado 37 37 0 0 0 37 0 0
zend cakephp 37 37 37 0 0 0 0 0
zend framework symfony 36 36 0 36 0 0 0 0
zend framework or cakephp 35 35 35 0 0 0 0 0
prado symfony 35 0 0 35 0 35 0 0
seagull vs cakephp 34 0 34 0 0 0 34 0
zend framework versus cakephp 32 32 32 0 0 0 0 0
Total 4568 3369 2515 1704 687 564 34 0
Interest Ratio
73.75% 55.06% 37.30% 15.04% 12.35% 0.74% 0.00%

Written by Seva

2009-05-15 (May 15) at 01:33:24

Prototype styled Google Analytics javascript snippet

leave a comment »

Recently I’ve created a prototype styled javascript snippet to enable google analytics on one of my sites. Save this code in google.analytics.js file and include it from the head tag of the page just after including of the prototype.js:

gaTrackerId = 'ss-ddddddd-d'; // insert your tracker id here
document.observe('dom:loaded', function() {
    var gaJsHost = (
        'https:' == document.location.protocol
            ? 'https://ssl.'
            : 'http://www.'
    ) + 'google-analytics.com/ga.js';
    var script = new Element('script', { 'src': gaJsHost});
    var gaTrack = function() {
        if (
            !script.readyState
            || /loaded|complete/.test(script.readyState)
        ) {
            var pageTracker = _gat._getTracker(gaTrackerId);
            pageTracker._trackPageview();
        }
    };
    script.observe('load', gaTrack);
    script.observe('readystatechange', gaTrack);
    document.body.appendChild(script);
});

Written by Seva

2008-08-11 (August 11) at 08:00:07