Check if a php method is called statically or as an instance method

PHP allows to call you any method of a class both as static and as instance method. Recent version of PHP is more strict though. But still you can call non-static function as static. Calling a non-static function that uses $this statically will cause a fatal error. You’d  want to catch this and throw exception with meaningful message.

If you can determine how this method is called whether statically or as instance method you can handle this situation.

See this example class.

class C {
    private $value = "value";
    public function method () { 
        echo __METHOD__. "\n"; 
        echo "Value:". $this->value. "\n"; 

This method will raise a fatal error you if call it as C::method().

To determine if its this is called statically or as an instance method there are two techniques.

  1. Using debug_backtrace
    1. class A { 
       public function m(){ 
       $bt = debug_backtrace(); 
       throw new Exception(__METHOD__." is called statically");
  2. Using isset on $this.
    1. class A { 
       public function m(){ 
       throw new Exception(__METHOD__." is not called from class instance");


The last one is shorter. But first one is more explicit.

Data extraction from external url via php made easy

How many times you extracted data from other website to include in your site? I guess its many times. In this web 2.0 era we all mix up different type of contents to make our own. Sometimes you grab data from, sometimes and then you create a mesh up. All these things need little bit of data parsing knowledge. Also you need to interact with an http resource. Many PHP coders does it by curl, DOMDocument etc extensions. This job is quite tedious. To resolve this problem I created a class long time ago. Now I put them on The classes I am talking about can be found on

“dxtool” stands for data extraction tools. Its very easy to use. Here I dump the README file from github.


  • php5
  • php5-curl extension
  • php5-json extension (already included with php5)


  • Extract Data from any http resource
  • Use simple regular expression to extract data
  • Hassle free http transaction
  • Supports cookie (via curl)
  • Can cache http response

Here is an example on how to use it.

require 'DataExtractor.php';
require 'WebGet.php';
$google_new_feed = '';
$w = new WebGet();
$content = $w->requestContent($google_new_feed);
$dx = new DataExtractor($content);
$dx->titles = '|title>([^<]+)</title|a';
$dx->rsstitle = '|title>([^<]+)</title|';
$data = $dx->extractArray();

If you run it you’ll see this output. Output will be different as google news rss will change over time

    [titles] => Array
            [0] => Top Stories - Google News
            [1] => Top Stories - Google News
            [2] => Draft of Lokpal Bill discussed informally at cabinet meet - Hindustan Times
            [3] => cabinet clears Food Security Bill - Hindustan Times
            [4] => Obama hails Havel's 'moral leadership' and 'dignity' - AFP
            [5] => Philippines struggles to cope after storm leaves 650 dead -
            [6] => Civil nuclear liability rules balanced, India to Russia - Hindustan Times
            [7] => 'Include Muslims, expand OBC quota' - Hindustan Times
            [8] => Sadhbhavana's success answer to Gujarat detractors: Narendra Modi - Daily News & Analysis
            [9] => Female protestor's beating sparks Egypt outrage -
            [10] => Romney says US withdrawal from Iraq 'precipitous' - AFP
            [11] => Come clean on Chidambaram's alleged favours to ex-client: BJP to Centre - The Hindu

    [rsstitle] => Top Stories - Google News

HOWTO: Convert 6 digit css color code to 3 digit

Okay, So you want to convert 6 digit css color codes to 3 digit. They are almost same. But 3 digit helps to remember it.  Before jumping into code let me explain what does 3 digit represents. A css color #abc means #aabbcc. No its NOT  #a0b0c0. It may appear that  the second one is more appropriate. But the fact is Its not. You can test it.   See the following table.

#aabbcc #abc #a0b0c0

So to convert any 6 digit css color to a 3 digit color needs some calculation. Thats why I have written a php function.

See the code bellow

function convert_color($color){
 preg_match("|#([\da-h]{2})([\da-h]{2})([\da-h]{2})|", $color, $match);
 foreach($match as $m)
  array_push($n, reduce_digit($m));
 return "#". implode("", $n);

function reduce_digit($hex){
 $n = hexdec($hex);
 $r = $n%17 ;
 $d = intval($n/17)+ (($r<8)?0:1);
 return dechex($d);

Just call the convert_color function with 6 digit css color. For example. “#bcd465”. Here # is necessary. This will return the 3 digit css color.

If you want to test it, run the following code.

$params= array(

foreach($params as $param){
    $f = convert_color($param[0]);
    $e = $param[1];
    echo "Passed={$param[0]}, Expected=", $e, ", Actual=";
    echo $f, ", Status=", (($e==$f)?"SUCCESS":"FAILED"), PHP_EOL;

I have run it it the result is good.

Passed=#aabbcc, Expected=#abc, Actual=#abc, Status=SUCCESS
Passed=#112233, Expected=#123, Actual=#123, Status=SUCCESS
Passed=#456789, Expected=#468, Actual=#468, Status=SUCCESS
Passed=#1234fa, Expected=#13f, Actual=#13f, Status=SUCCESS
Passed=#000000, Expected=#000, Actual=#000, Status=SUCCESS