This is about simple random walk (as defined in theory). Uses the cairo library. A ball walks randomly on the screen and leaves a trace behind.
100819 items (213 unread) in 22 feeds
CNN
(5 unread)
MSNBC
(8 unread)
PHP
(62 unread)
Deals
(101 unread)
Web Development
(35 unread)
CNN Money
(1 unread)
Frugal Blogs
(1 unread)
This is about simple random walk (as defined in theory). Uses the cairo library. A ball walks randomly on the screen and leaves a trace behind.
This is about simple random walk (as defined in theory). Uses the cairo library. A ball walks randomly on the screen and leaves a trace behind.
" />
Defined probability for the next step to walk is 0.5. Probabilities for left step and other parameters are pre-defined however someone can change them from inside the constructor at will. Accepted probabilities are from 0 to 1, also -1 is accepted, this indicates that the program choses on its own the probability for the next step to walk. The purpose of this is to show the the usage of cairo library!
<?PHP
//Definition of widgets
$scrolled_win = new GtkScrolledWindow();
$textview = new GtkTextview();
$buffer = new GtkTextBuffer();
$win = new GtkWindow();
//Modify widgets
$scrolled_win->set_policy(1,1);
$textview->modify_font(new PangoFontDescription('Courier New 14'));
$win->maximize();
$win->connect_simple('destroy', array('Gtk', 'main_quit'));
//Pack widgets
$scrolled_win->add($textview);
$textview->set_buffer($buffer);
$win->add($scrolled_win);
/*** Get tags table ***/
$tag_table = $buffer->get_tag_table();
/*** Create and add tag ***/
//underline
<?php
// Definition of widgets
$scrolled_win = new GtkScrolledWindow();
$textview = new GtkTextview();
$buffer = new GtkTextBuffer();
$win = new GtkWindow();
// Modify widgets
$scrolled_win->set_policy(1,1);
$textview->modify_font(new PangoFontDescription('Courier New 14'));
$win->maximize();
$win->connect_simple('destroy', array('Gtk', 'main_quit'));
// Pack widgets
$scrolled_win->add($textview);
$textview->set_buffer($buffer);
$win->add($scrolled_win);
/*** Get tags table ***/
$tag_table = $buffer->get_tag_table();
/*** Create and add tag ***/
// underline
Before we dive into the code refactoring, I first want to step back and take a look at why you would like to use a framework instead of keeping your plain-old PHP applications as is. Why using a framework is actually a good idea, even for the simplest snippet of code and why creating your framework on top of the Symfony2 components is better than creating a framework from scratch.
I won't talk about the obvious and traditional benefits of using a framework when working on big applications with more than a few developers; the Internet has already plenty of good resources on that topic.
Even if the "application" we wrote yesterday was simple enough, it suffers from a few problems:
<?php // framework/index.php $input = $_GET['name']; printf('Hello %s', $input);
First, if the name query parameter is not given in the URL query string,
you will get a PHP warning; so let's fix it:
<?php // framework/index.php $input = isset($_GET['name']) ? $_GET['name'] : 'World'; printf('Hello %s', $input);
Then, this application is not secure. Can you believe it? Even this simple snippet of PHP code is vulnerable to one of the most widespread Internet security issue, XSS (Cross-Site Scripting). Here is a more secure version:
<?php $input = isset($_GET['name']) ? $_GET['name'] : 'World'; header('Content-Type: text/html; charset=utf-8'); printf('Hello %s', htmlspecialchars($input, ENT_QUOTES, 'UTF-8'));
As you might have noticed, securing your code with
htmlspecialcharsis tedious and error prone. That's one of the reasons why using a template engine like Twig, where auto-escaping is enabled by default, might be a good idea (and explicit escaping is also less painful with the usage of a simpleefilter).
As you can see for yourself, the simple code we had written first is not that simple anymore if we want to avoid PHP warnings/notices and make the code more secure.
Beyond security, this code is not even easily testable. Even if there is not much to test, it strikes me that writing unit tests for the simplest possible snippet of PHP code is not natural and feels ugly. Here is a tentative PHPUnit unit test for the above code:
<?php
// framework/test.php
class IndexTest extends \PHPUnit_Framework_TestCase
{
public function testHello()
{
$_GET['name'] = 'FabienTruncated by Planet PHP, read more at the original (another 16397 bytes)
In January 2011, the second iteration of the Plat_Forms contest had been conducted by FU Berlin Heise and OSBF. 16 Teams with 3 developers each were all given the same task. They had to implement as much as possible of the given requirements in a given time span in a controlled environment. After the event, the resulting code has been evaluated by the team around Prof. Lutz Prechelt and Ulrich Stärk. The results were then presented on November 25, 2011.
For the PHP Side, i was asked to select the participants. To provide a good coverage of current PHP best practices, i selected 2 zend framework teams, 1 symfony team and a flow3 team (as an additional influence besides the general purpose frameworks).
After the great success of PHP in the previous contest in 2007, we expected after all the improvements in the PHP space during the last years, that PHP would be even more successful than before. But we were supprised:

So how can this be interpreted?
Java was almost constant, slightly improved coverage. Perl decreased somewhat, that might be discussed elsewhere. Ruby was very good, but there was no numbers for 2007 so no trend can be derived here.
But what happened to PHP? Less coverage than 2007 and much less consistency between the teams (one quite good, the other 3 rather bad). Did we select the wrong teams? Did they have a bad day?
Why did all the work the PHP community did during the last years in terms of software architecture, frameworks and quality not lead to better productivity?
Ok, the results for robustness have improved compared to 2007 for PHP. But the size of PHP applications was less consistent than in 2007: Some PHP applications were quite compact, others were as big as the Java applications but while covering less functionality.
To me these results match my observations during the last years in PHP space. The idea of PHP being "super-productive" needs to be questioned at least. Maybe the PHP community is on a journey and the destination has not been reached yet. We will see with the results of the next iteration of this contest...
Side Notes:
While Ruby Teams spent much more time writing automated tests than everybody else, they still got the highest functional coverage.
While in 2007 the PHP teams were most interested in customer's wished, in 2011, Ruby asked the most detail questions.