Tonton MyTV dilengkapi intisari rancangan pada smart TV bermula RM4.99 sebulan. Selanjutnya →

New PHP

Ahad, 23 Jun 2013, 12:01 am0

PHP is an amazing server side language to build web application. The ability to embed an executable code into text content is really practical in the web page generation, thus the most convenient language to use to create dynamic website. Additionally, it’s also easy to learn by beginner.

However, the language inconsistencies in naming, function output, default behavior etc. – is a real PITA to seasoned developers. One of the inconsistencies that I’d like to address here is the notation syntax used. Namespace declaration, variable name, methods and properties accessor & also array – they are all different. Some use backslash, some use arrow, some use double colon – as a result of borrowing syntax from a lot of other languages.

Therefore i hope in PHP 6 there’ll be some groundbreaking changes to its syntax. In my opinion, to be a much cleaner syntax, just use one format – dot notation. Here’s the new code syntax I suggest, together with some new features that I feel useful to have in new PHP

Current:

// namespace
namespace orgexamplesystem;

use orgexamplesystembase;
use orgexamplestorage;
use orgexamplecollectionDictionary;

// class
class Application extends baseApplication {

  private static $mode = 0;
  private $id = 0;
  private $db;

  public function __construct() {

    // method / property accessor
    $this->id = 1;
    $this->run();

    // namespaced class
    $this->db = new storageDatabase();
    $collection = new Dictionary(array('a','b'));

    // array
    $arr = array(
      'a' => 'A'
    );

    // array php 5.4
    $arr = [
      'b' => 'B'
    ];

    // array operation
    array_pop($arr);
    array_push($arr, 'c');

    // strings
    $str = 'name';
    $n = $str[0];
    $lower = strtolower($str);

    // include files
    require '../lib/functions.php';
    require_once '../lib/functions.php';
    include '../lib/functions.php';
    include_once '../lib/functions.php';

    // static method / property accessor
    $mode = self::$mode;
    $dict = Dictionary::create();

  }

  public function run() {

    // parent class accessor
    parent::run();

  }

}

New PHP:

// namespace namespace org.example.system; use org.example.system.base; use org.example.storage; use org.example.collection.Dictionary; // class class Application extends base.Application { private static mode = 0; private id = 0; private db; public function __construct() { // method / property accessor this.id = 1; this.run(); // namespaced class this.db = new storage.Database(); collection = new Dictionary(['a', 'b']); // array can be treated as mixed of hashed map, // dictionary, list or set // array is now an object type arr = [ 'b' : 'B' ]; // array operation arr.pop(); arr.push('c'); arr[0]; arr[0:2]; arr[:2]; // string is now object type, // not treated like an array str = 'name'; str.charAt(0); str.substr(0, 1); str.toLower(); // include files, use 'use' statement // all diff statement behavior will use 'include' behavior // use full path use com.example.lib.utils; use com.example.lib.functions as fn; // class alias fn.log('message'); // static method / property accessor mode = self.mode; dict = Dictionary.create(); } public function run() { // parent class accessor parent.run(); } }

Sure, it looks like Java (or Javascript, or Python), nothing wrong with that, as long as the primary feature – embeddable code – remain intact, PHP will always be the main choice for web app development.

Tulis komen: