Tag Archives: PHP

VZ @ IPC und WebTech

Vom 15.11. bis zum 18.11. finden nächste Woche in Karlsruhe die International PHP conference und die Webtech Conference statt. VZ Netzwerke unterstützt diese Konferenzen als Silber Sponsor und ist mit einem Stand, vier Sessions und einer Keynote vertreten:

IPC Logo IPC Logo

  • Push-Implementierung in der studiVZ iPhone App
    Max Horváth, Steffen Irrgang
    In dieser Session berichten wir, wie studiVZ die Push-Funktionalität auf der iPhone- und auf der Serverseite implementiert hat. Wir zeigen, wie der Client sich über das studiVZ-API anmeldet und über welche Architektur Push-Nachrichten auf dem iPhone landen. Dabei wird näher auf die AMQP-Queue-Infrastruktur von studiVZ eingegangen und gezeigt, welchen Fluss eine Benachrichtigung bei studiVZ durchlebt, bis sie auf dem iPhone eines Nutzers ankommt.

    Download Slides

    16.11.2009 | 09:45 – 10:45

  • OAuth – ein offener Standard für die sichere Authentifizierung in APIs
    Bastian Hofmann, Max Horváth, Andre Zayarni
    In dieser Session widmen wir uns dem offenen Standard OAuth. Er wird für die sichere Authentifizierung in APIs genutzt. Wir zeigen, wie man OAuth implementiert und wieso es sinnvoll ist, Drittanwendungen über OAuth an die eigene API anzubinden. Außerdem gehen wir darauf ein, wie eine eigene Webapplikation mittels OAuth mit anderen Services verbunden werden kann.

    Download Slides

    16.11.2009 | 14:30 – 15:30

  • OpenSocial in der Praxis
    Sebastian Galonska, Bastian Hofmann
    OpenSocial bietet vielfältige Möglichkeiten, die Funktionalität eines Social Networks zu erweitern. Dabei sind die Konzepte zur Entwicklung von Gadgets einfach zu erlernen. In dieser Session geben wir Einblicke, wie Gadgets für die VZ-Netzwerke, aber auch jeden anderen OpenSocial-kompatiblen Container, erstellt und veröffentlicht werden können. Dabei berücksichtigen wir auch Aspekte wie Skalierung und Sicherheit.

    Download Slides

    17.11.2009 | 10:30 – 11:30

  • Best Practices bei der Entwicklung von öffentlichen APIs
    Steffen Irrgang, Max Horváth, Andre Zayarni
    In dieser Session soll praxisnah erörtert werden, welche Fallstricke und vor allem welche Best Practices in Bezug auf die Entwicklung von öffentlichen APIs zu beachten sind. Dabei konzentrieren wir uns auf RESTful Web Services sowie auf den offenen Authentifizierungsstandard OAuth.

    Download Slides

    17.11.2009 | 16:30 – 17:30

  • Keynote: Bugfree, agil und überhaupt
    Jodok Batlogg
    In his keynote Jodok will highlight some insights on studiVZ. How important are agile processes? Is studiVZ really bugfree or is there a need for a bugtracking system? Why should one additional line of SQL require 10 additional servers and how can good profiling save you 20 of them?
    18.11.2009 | 11:45 – 12:30

PHP SPL Data Structures Benchmark

Data structures and collections are one of the most wanted features for the Standard PHP Library SPL over the last few years. With PHP 5.3 we’ll finally get a little of what we want and this is good news. With data structures like stack,  queue, heap or priority queue implemented in C we expect PHP programming to become somewhat more efficient.

Inspired by this post http://blueparabola.com/blog/spl-deserves-some-reiteration we decided to run our own benchmarks to either verify or disapprove the results posted. Our benchmarks were executed on a 64bit RHEL with PHP 5.3.0beta1. As you may expect, we carefully excluded startup or compilation time and measured only the code blocks in question. We used getrusage() to determine CPU time consumption. A huge number of iterations guaranteed smooth results.

The first structure under consideration was the SplFixedArray. If you only need numerical indices you now can create an array of fixed size that does not have to grow while more and more items are inserted. Dealing with an SplFixedArray saves you about 10 percent of runtime compared to a plain old PHP array.

Next we tried the SplStack and SplQueue. It is usually easy to implement a stack and a queue with plain arrays. Using array_push(), array_pop() and array_shift() is straightforward. It may be a surprise to the average PHP programmer to learn about the runtime behaviour of these functions. Worst is array_shift() because of the internal rehashing and the experienced PHP programmer may – for critical code at least – try to access arrays by indices maintaining counters, for example. This is much more efficient. Compared to the functions, at least SplQueue is something like an upset, but it is possible to find comparable solutions with plain PHP.

bars_full_ok

There is a little danger to compare apples and pears when turning towards SplHeap and SplPriorityQueue. What is the proper representation of a heap implemented using plain old arrays only? It’s a sorted array, ok. But a heap is sorted for each insert, so, do we really have to sort the array for each insert? Who will do this in real life?

It’s the use case that decides about the sorting strategy. If you are supposed to carefully separate writing the heap and reading from it, it is sufficient to sort it once. That way you beat SPL. But if you have to mix reading and writing arbitrarily the SPL will beat plain arrays by far. This is shown in the pictures below. For the mixed strategy we read once for 5 inserts and the SplMinHeap scales very well. The same holds for SplMaxHeap and SplPriorityQueue.

splminheap_rw_separated

splminheap_rw_mixed

Lessons learned:

  • SPL rules
  • use SPL data structures where appropriate for a particular use case, they are efficient and comfortable
  • benchmarking is error prone
  • anyway, always benchmark performance critical code blocks

PHP Bug by Design

Check the following code snippets. They use square bracket syntax to index data in an array and a string.

$array = array(1,2,3);
print $array[0];
>> 1
print $array['foo'];
>> Notice: Undefined index:  foo

$string = '123';
print $string[0];
>> 1
print $string['foo'];
>> 1

If you access a non existing index in an array you will be notified about a non existing index and NULL is returned. That’s fine so far. But what if you’re doing the same with a string? Is a string an array of characters as you might expect?

Unfortunately not in PHP. A string is a string and accessing a non existing index in a string is a different thing. PHP obviously converts the string index “foo” into an integer since strings have only numerical indices. Casting “foo” into integer is 0 and the character at position 0 is returned – without any notice. A string is not an array of characters.

As you may read here http://bugs.php.net/bug.php?id=44914 this is also fine for the creators of PHP. For me, I’m sorry, it’s a reason to stop using PHP (if I could at least). Implicit type casts in PHP are a story on its own and notoriously leading to subtle and hard to find bugs. But this one is beyond belief.

The only way to protect you from a catastrophe is to check at any and every usage of arrays if you really have an array. Type hint arrays. Use is_array() excessively. Write thousands of tests.

Better choice – use a well designed programming language. A better supported at least.