Tuesday, August 28, 2012

Singleton with PHP

        Singleton pattern is under creational design patterns which deals with mechanisms of creating objects according to the situation. I recently used this pattern with PHP. It's very important to get to know use of this pattern.

              Singleton pattern can be used for always keep one object of a specific class while an application is running. It save the memory which is going to allocate for new objects of the class in every where inside the application. Singleton practically used for logger classes which provide global logging access point whithout logging each and every application after the main loggin operation, configuration classes which consist configuration data of an application, access resources which are in shared mode like multi threading applications, Factory design pattern which dynamically create the objects etc.

         It's very easy to implement singleton with PHP (as well as other languages). What we need to do is make a private constructor and control the creating object of this class by a public method. Following code snippet shows how to do this.

class MySingleton {

    private static $instance = null;

    public static function getInstance() {
        if(is_null($this->instance)) {
            $this->instance = new MySingleton($params);
        }
        return $this->instance;
    }

    private function __construct($params) {
        // set parameters
    }

    public function doSomething() {
        // some code
    }

}

         Since the constructor is private only the class itself can be access the constructor. getInstance method is accessible for anywhere and since that is a static method it can be used anywhere without creating an object of this class. This function handles the situations of the need of a new instance of MySingleton class by calling the private constructor if needed. Static variable is used because of the restriction of using non-static fields inside static functions.

In the places where needs to call doSomething method in MySingleton class, we call as,
MySingleton::getInstance()->doSomething()
or, if the functions inside the class is frequently used,
$my_singleton = MySingleton::getInstance();
$my_singleton ->doSomething()

It's very simple. Hope this helpful. Have a fun with singleton. :)

Saturday, August 25, 2012

සැරයටියෙනුත් යනෙනා තුරා.....

කෙලිලොලෙන් ගතකරපු අපේ ඒ අතීතය.....
මතකයක් පමණක්ම කර,
ඇයිද නුඹ අපව තනිකර ගිලිහුණේ.....

ඒ අතීතය,
හැමදාමත් අද ඊයේ වගේ දැනේවි,
නුඹේ කතා කනට ඇසේවි,
නුඹේ සිනහව හිතේ මැවේවි,

ඒත්.....
නුඹේ කතාව, ඒ සිනහව
මතු කිසිදිනෙක අප හැබැහින්ම නොවිදීවි.....

නුඹ දමා ගියත් අපව,
මිතුරෙකුව හදේ රැදී සිටී හැම දිනෙක.....

මතු භවයෙදිත් අප මිතුදමින් බැදෙමු
ඒත්.....
මෙලෙස අකාලයේ ලොවම හැර දමා යන්නට නොව,
සැරයටියෙනුත් ගමන් කරනා තෙක්ම.....

Monday, August 20, 2012

GSoC 2012 - 13th Week Progress

         This is the last week we interact with the coding and documenting under GSoC 2012 official time line. In my last week I implemented the feature request simple sort for process list. And improved some tests for PMA_DisplayResults class.

                In this week I did some improvements in tests of PMA_DisplayResults class. As the documentation, there were not much things to mention as details in Documentation.html. I only mentioned about the feature of displaying multiple results for queries as well as stored procedures. While I'm implementing the functions phpdoc comments and block comments were improved. So those things make me easy in documenting session. A feature request I proposed to do at the end of the last week was already fixed (My patch has been accepted for that). So I didn't involve with that.

   Dieter has suggested to change the class structure for PMA_CommomFunctions class. After discussing in mailing list I decided to make any change after the GSoC 2012 session. I will be stop my further work related to code and documentation improvements under GSoC 2012 session by today.

    In next week I'll submit my evaluation form. I wish all GSoCers who passed the mid evaluations to get success this Great event GSoC 2012 with Open source :)

Monday, August 13, 2012

GSoC 2012 - 12th Week Progress

      Its more one week to do the remaining work of GSoC 2012 project. Last week I introduced new links inside information_schema database with modifying a previous code for showing links inside mysql database. As well support for MIME transformations in query results and VIEWs were implemented. More improvements related to VIEWs were done.

           In this week I implement the feature request simple sort for process list. That was little older feature request and the description of the feature request itself suggested the fix. But with the time those files were changed and renamed. So I improve the behavior of server_status.php file to do the required thing. Since the sorting needs to specify the table, I used prcesslist table of information_schema database. Apart from these I did some test implementations for the functions in PMA_DisplayResults class.

           In my next week I'm intending to improve some tests for PMA_DisplayResults classwhile doing any remaining minor improvements in my codes.

Monday, August 6, 2012

GSoC 2012 - 11th Week Progress

             This is my 11th week of GSoC 2012 project. Its one more week to active work in code level. Last week I did some improvements on showing syntax highlighted SQL statement in PROCESSLIST table in information_schema database and introduce some links inside mysql database related to displaying data fields.

            This week as the first task I introduce some links inside information_schema database. In previous week I implement same kind of thing but in more specific way to that task. So I change the previous implementation where it can be used to my new task. I changed the structure of the global variable $GLOBALS['mysql_schema_relation'] and renamed it as $GLOBALS['special_schema_links'] and renamed the file as special_schema_links.lib.php. The new structure of the array can be represented by following snippet.

$GLOBALS['special_schema_links'] = array( 
    // Database name is the major element
    'mysql' => array(
        // Table name
        'db' => array(
            // Column name
            'user' => array(
                // Main url param (can be an array where represent sql)
                'link_param' => 'username',
                // Other url params
                'link_dependancy_params' => array(
                    0 => array(
                        // URL parameter name
                        // (can be array where url param has static value)
                        'param_info' => 'hostname',
                        // Column name related to url param
                        'column_name' => 'host'                        
                    )
                ),
                // Page to link
                'default_page' => 'server_privileges.php'
            )
        )
    )
);

By using this array, links can be generated where needed specially inside schema like mysql, information_schema etc.

          Next I moved into implement feature request for support MIME transformations in query results. Some of the query results which show only field with transformation and large data (blob fields etc) were not displayed their expected transformation in results. PMA has avoided that kind large data comparisons for SQL queries. So that, $where_clause parameter is not generating properly. So, assuming the length of the data fields will not same, I used the length of the data field to generate proper $where_clause and compare inside SQL query. Now query results display the expected out put with their transformations if any.

            Then I moved into give the support for transformed data for VIEWs. In PMA the columns with any transformation are stored inside column_info table of phpmyadmin database. But the problem was these information were not stored in column_info table while creating VIEWs. So I did some changes to store needed details of transformations in VIEWs whie creating them by the link (create view) in browse mode of a result set. A new file called tbl_views.lib.php was introduce to place related functions.

        As well when deleting a column, table, view or database the related information on transformations were not deleted from the column_info table. So I implement a function to delete relevant entries and placed the necessary places to do the work.

In my next week I intend to implement the feature request on simple sort for server_processlist.php.