Programmer PHP Memilih Windows Daripada Linux

Dari The Register: menurut survey yang dilakukan Zend Technologies terhadap lebih dari 5 ribu koresponden pada Desember 2009 lalu, didapati 42% programmer PHP memilih Windows sebagai OS untuk development mereka. Sementara yang memilih Linux di urutan kedua sebanyak 38,5%, dan Mac OS X di urutan ketiga dengan 19,1%.

Tapi angka ini berbanding terbalik untuk server yang dipilih mereka untuk hosting aplikasinya. 85% dari koresponden memilih Linux sebagai server, dan hanya 11% yang memilih Windows. Sementara Max OS X server cuma 2% saja.

Menurut saya angka ini cukup akurat menggambarkan kenyataan di lapangan. Tapi Windows jadi OS pilihan untuk development menurut saya bukan karena IIS-nya, tapi karena kebanyakan programmer masih terbiasa dengan Windows, entah karena program-program pendukungnya (seperti Editor) atau program-program lain yang tidak berhubungan dengan programming. Belum lagi kebanyakan karena tidak ada pilihan lain, karena cuma Windows yang terinstal di kantor tempat ia bekerja misalnya. :)

PHP 5 Tetap Yang Tercepat

Sebastian Bergmann kembali melakukan benchmark terhadap PHP untuk berbagai versi (PHP 3.0 s/d PHP 5.3-CVS) yang diinstal dengan GCC 3.4.6 dan GCC 4.1.1 untuk berbagai flag optimisasi ( -O{0|1|2|3|s} ). Yap, total instalasi yang doi lakukan sebanyak 80 buah PHP binary, *sinting*.

Dengan menggunakan bench.php yang dimodifikasi (bench.php baru ada di Zend Engine 2/PHP 5 jadi dia memodif untuk PHP versi sebelumnya), dia mendapatkan data kecepatan untuk tiap versi PHP seperti terlihat pada grafik berikut:

Nggak usah terlalu lama mikir, PHP 5 tetap yang tercepat. Dan yang menarik PHP 5.3 yang tidak lama lagi dirilis memiliki performansi yang menggiurkan.

Perlu dicatat, seluruh konfigurasi PHP yang diinstal pada percobaan ini dengan men-disable seluruh modul (–disable-cgi –disable-all). Bayangkan kalau ditambah dengan modul seperti APC atau Zend Optimizer.

Speedy Alert

Koneksi Telkom Speedy dipengaruhi ketinggian laut? hmm, nggak ngerti deh bagaimana hubungannya. Tapi itu yang dibilang petugas Telkom saat menginstal Telkom Speedy di rumah saya kemarin. Memang jarak rumah cuma beberapa ratus meter dari pinggir laut. Tapi sejauh ini nggak ada masalah tuh. Dan cukup mengejutkan bagi saya, ternyata koneksinya lumayan cepat, hehe.

Eniwey, kalau anda seperti saya yang nggak pernah berhasil pake Speedy Alert, skrip berikut mungkin berguna untuk memantau penggunaan Speedy. Oya, pakai Zend Framework ya.

function getSpeedyUsage($username,$password)
{
  $url = "http://portal.telkomspeedy.com/index.php?flash=-1";

  $client = new Zend_Http_Client($url, array(
    'maxredirects' => 0,
    'timeout'      => 30));

  $client->setParameterPost(array(
    'username'       => $username,
    'password'      => $password,
    'login'             => '    Login    '
  ));

  $response = $client->request('POST');
  $body = $response->getBody();

  $html = str_replace("\n","",$body);
  preg_match_all("|<td class=\"data\"[^>]+>(.*)</td>|U",$html,$regs);

  $info = array();
  $info['pemakaian']['persen'] 	= $regs[1][1];
  $info['pemakaian']['besar'] 	= $regs[1][3];
  $info['limit'] = $regs[1][4];
  $info['kelebihan'] = $regs[1][5];

  return $info;
}

Penggunaannya seperti berikut:

<?php
require_once 'Zend/Http/Client.php';

/************** ubah di sini **************/
$username = "XXXXXXXXXXXX@telkom.net";
$password = "hewhomustnotbenamed";
/******************************************/

$info = getSpeedyUsage($username,$password);

print_r($info);

?>

Keluarannya kurang lebih seperti ini:

Array
(
    [pemakaian] => Array
        (
            [persen] => 17,93%
            [besar] => 179,28 MB
        )

    [limit] => 1.000 MB
    [kelebihan] => 0,00 MB
)

PHP5 Goes Mainstream

If you’re planning to build an apps in PHP4, then you must have missed this announcement,

The PHP development team hereby announces that support for PHP 4 will continue until the end of this year only. After 2007-12-31 there will be no more releases of PHP 4.4. We will continue to make critical security fixes available on a case-by-case basis until 2008-08-08. Please use the rest of this year to make your application suitable to run on PHP 5.

Yeah, finally. Let’s start a new day with PHP5. Don’t worry about the things, you’re not alone. And we have a fantastic framework too.

Understanding Zend_Controller (Part I)

Zend_Controller is the heart of Zend Framework's MVC system. To understand why and how Zend_Controller works you need to have a basic knowledge of MVC. But you'll get more in-depth idea of MVC by starting to write codes anyway.

So, let's start by creating file system layout. Here's my simple layout:

application/
  controllers/
    IndexController.php
    FooController.php
  views/
    scripts/
      index/
        index.phtml
      foo/
        bar.phtml
library/
  Zend/
.htaccess
index.php

To work with Zend_Controller you need to create a rewrite roules in .htaccess file,

RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css)$ index.php

I'm assuming that we use Apache mod_rewrite here, you might want to look other configuration for different web server here.

index.php is our bootstrap file where all requests are routed through. It controls all the workflows. Open up your editor and write this code,

<?php
if ( ! defined( "PATH_SEPARATOR" ) ) {
  if ( strpos( $_ENV[ "OS" ], "Win" ) !== false )
    define( "PATH_SEPARATOR", ";" );
  else
    define( "PATH_SEPARATOR", ":" );
}

set_include_path(get_include_path() . PATH_SEPARATOR . "library");

require_once 'Zend/Controller/Front.php';
Zend_Controller_Front::run('application/controllers');

Zend_Controller_Front::run is a shortcut to tell the controller to search controller files in application/controllers directory for each request and dispatch it.

By default, the first segment of a URL path maps to a controller, and the second to an action. For example, given the URL http://www.example.com/foo/bar, the path is /foo/bar, which will map to the controller foo and the action bar. If no action is provided, the action index is assumed, and if no controller is provided, the controller index is assumed (following the Apache convention that maps a DirectoryIndex automatically).

So, http://www.example.com/foo will map to the controller foo and the action index. While http://www.example.com will map to the controller index and the action index.

By default, the controller files reside on the directory which we declared in Zend_Controller_Front::run method. The file name use the the controller name with uppercase on the first letter added with Controller words. For example, URL http://www.example.com/foo will search FooController.php in application/controllers directory. While http://www.example.com will search IndexController.php.

The next step is to write the action controller, the actual code for handling the request. First, let's open up application/controllers/IndexController.php and enter the following,

<?php
require_once 'Zend/Controller/Action.php';

class IndexController extends Zend_Controller_Action
{
  public function indexAction()
  {
  }
}

As you might suspect, indexAction method is the action controller, where executed for index action, i.e where the URL is http://www.example.com

Similarly, you can handle URL http://www.example.com/foo/bar by writing this code into application/controllers/FooController.php,

<?php
require_once 'Zend/Controller/Action.php';

class FooController extends Zend_Controller_Action
{
  public function barAction()
  {
  }
}

Now, if you access http://www.example.com/foo/bar you'll get,

Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (error)' in ...

Good, don't worry about the error. It tells you that you're missing something. But before we get to that, it is a good practice to write a controller to handle such errors. By default, Zend_Controller will execute errorAction() method in ErrorController.php when it can't find the appropriate controller files or when something is wrong. So open up application/controllers/ErrorController.php and write the following,

<?php
require_once 'Zend/Controller/Action.php';

class ErrorController extends Zend_Controller_Action
{
  public function errorAction()
  {
    die("An error occurred; please try again later.");
  }
}

Now, if you access URL like this http://www.example.com/none/nothing, you'll get,

An error occurred; please try again later.

No, you can't treat this as 404 Not Found like. Because it's executed not only when the controller can not be found, but also when there's something missing in your codes. For example when you forget the view parts.

Right, by default ViewRenderer action helper is enabled. What this means is that by simply defining an action method and a corresponding view script, you will immediately get content rendered. By default, Zend_View is used as the View layer in the MVC. The ViewRenderer does some magic, and uses the controller name (e.g., foo) and the current action name (e.g., bar) to determine what template to pull. By default, templates end in the .phtml extension, so this means that, http://www.example.com/foo/bar will render the template in views/scripts/foo/bar.phtml.

So, let's open up views/scripts/foo/bar.phtml file and write the following templates,

<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  <title>My first Zend Framework App</title>
</head>
<body>
    <p>You say foo bar!</p>
</body>
</html>

And now you can view your site http://www.example.com/foo/bar, yes it says

You say foo bar!

Great!

That's it. Now you have the basic concept of how Zend_Controller works. As you might notice, i say "by default" many times, this is because you can change the way it works according to your needs. All by extending the abstract and interface class. We'll talk more about this on later post.

Setting Up Zend Framework

I still see some people having a hard time using Zend Framework, so i'll share some of my experience using ZF. Hope this will help.

Let's start with the easy and basic one, setting up ZF. There's two simple way of doing this:

First, download the latest stable release here. It's available in both .zip and .tar.gz formats. Extract the files.

Second, once you have a copy of the ZF available, your application needs to be able to access the framework classes (in library/Zend folder). There are several ways to do this, but assuming you don't have control over PHP configuration in your server all you need to do is using set_include_path function to include the ZF classes.

If you're using Linux,

set_include_path(get_include_path().":library");

or in Windows,

set_include_path(get_include_path().";library");

or you can make your apps compatible with both OS, by determining the path separator, like this:

if ( ! defined( "PATH_SEPARATOR" ) ) {
  if ( strpos( $_ENV[ "OS" ], "Win" ) !== false )
    define( "PATH_SEPARATOR", ";" );
  else
    define( "PATH_SEPARATOR", ":" );
}

set_include_path(get_include_path() . PATH_SEPARATOR . "library");

That's it, you're set to go.