Friday 21 May 2010

What are the differences b/w GET and POST in ajax call?

var http = new XMLHttpRequest();

Using GET method

Now we open a connection using the GET method.

var url = "getdata.php";
var params = "name=ashwani&action=true";
http.open("GET", url+"?"+params, true);
http.onreadystatechange = function() {//Call a function when the state changes.
 if(http.readyState == 4 && http.status == 200) {
  alert(http.responseText);
 }
}
http.send(null);
I really hope that this much is clear for you - I am assuming that you know a bit of Ajax coding.



POST method

We are going to make some modifications so POST method will be used when sending the request...

var url = "getdata.php";
var params = "name=ashwani&action=true";
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
 if(http.readyState == 4 && http.status == 200) {
  alert(http.responseText);
 }
}
http.send(params);

The first change(and the most obvious one) is that I changed the first argument of the open function from GET to POST. Also notice the difference in the second argument - in the GET method, we send the parameters along with the url separated by a '?' character...

Cheers!

How many NULL values can Unique key contains?

An unique key can contains unlimited NULL values because each NULL value differ from another NULL value.

Cheers!

How to Speed Up Web Site?

There are the following steps through which you can speed up you web site:


  • Minimize HTTP Requests
  • Add an Expires or a Cache-Control Header
  • Gzip Components
  • Put Stylesheets at the Top
  • Put Scripts at the Bottom
  • Avoid CSS Expressions
  • Make JavaScript and CSS External
  • Minify JavaScript and CSS
  • Avoid Redirects
  • Remove Duplicate Scripts
  • Make Ajax Cacheable
  • Flush the Buffer Early
  • Make favicon small and cacheable
  • Use GET for AJAX Requests

  • Reduce the Number of DOM Elements

     

           The number of DOM elements is easy to test, just type in Firebug's console:    

           document.getElementsByTagName('*').length 

Cheers!

Thursday 20 May 2010

What are Constructors and Destructors?

Constructor

void __construct ([ mixed $args [, $... ]] )
PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used. 

<?phpclass BaseClass {
   function 
__construct() {
       print 
"In BaseClass constructor\n";
   }
}

class 
SubClass extends BaseClass {
   function 
__construct() {
       
parent::__construct();
       print 
"In SubClass constructor\n";
   }
}
$obj = new BaseClass();$obj = new SubClass();?>
 
 

Destructor

void __destruct ( void )
PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as all references to a particular object are removed or when the object is explicitly destroyed or in any order in shutdown sequence. 


<?phpclass MyDestructableClass {
   function 
__construct() {
       print 
"In constructor\n";
       
$this->name "MyDestructableClass";
   }

   function 
__destruct() {
       print 
"Destroying " $this->name "\n";
   }
}
$obj = new MyDestructableClass();?>

 

What is static keyword?

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static can not be accessed with an instantiated class object (though a static method can).
For compatibility with PHP 4, if no visibility declaration is used, then the property or method will be treated as if it was declared as public.
Because static methods are callable without an instance of the object created, the pseudo-variable $this is not available inside the method declared as static.
Static properties cannot be accessed through the object using the arrow operator ->.
Calling non-static methods statically generates an E_STRICT level warning.
Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.
As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static). 

<?phpclass Foo{
    public static 
$my_static 'foo';

    public function 
staticValue() {
        return 
self::$my_static;
    }
}

class 
Bar extends Foo{
    public function 
fooStatic() {
        return 
parent::$my_static;
    }
}


print 
Foo::$my_static "\n";
$foo = new Foo();
print 
$foo->staticValue() . "\n";
print 
$foo->my_static "\n";      // Undefined "Property" my_static
print $foo::$my_static "\n";$classname 'Foo';
print 
$classname::$my_static "\n"// As of PHP 5.3.0
print Bar::$my_static "\n";$bar = new Bar();
print 
$bar->fooStatic() . "\n";?>

What is interface?

Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.
Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined.
All methods declared in an interface must be public, this is the nature of an interface. 


<?php
// Declare the interface 'iTemplate'interface iTemplate{
    public function 
setVariable($name$var);
    public function 
getHtml($template);
}
// Implement the interface
// This will work
class Template implements iTemplate{
    private 
$vars = array();
 
    public function 
setVariable($name$var)
    {
        
$this->vars[$name] = $var;
    }
 
    public function 
getHtml($template)
    {
        foreach(
$this->vars as $name => $value) {
            
$template str_replace('{' $name '}'$value$template);
        }

        return 
$template;
    }
}
// This will not work
// Fatal error: Class BadTemplate contains 1 abstract methods
// and must therefore be declared abstract (iTemplate::getHtml)
class BadTemplate implements iTemplate{
    private 
$vars = array();
 
    public function 
setVariable($name$var)
    {
        
$this->vars[$name] = $var;
    }
}
?>

What is abstract class?

PHP 5 introduces abstract classes and methods. It is not allowed to create an instance of a class that has been defined as abstract. Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature they cannot define the implementation.
When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private. 

<?phpabstract class AbstractClass{
    
// Force Extending class to define this method
    
abstract protected function getValue();
    abstract protected function 
prefixValue($prefix);

    
// Common method
    
public function printOut() {
        print 
$this->getValue() . "\n";
    }
}

class 
ConcreteClass1 extends AbstractClass{
    protected function 
getValue() {
        return 
"ConcreteClass1";
    }

    public function 
prefixValue($prefix) {
        return 
"{$prefix}ConcreteClass1";
    }
}

class 
ConcreteClass2 extends AbstractClass{
    public function 
getValue() {
        return 
"ConcreteClass2";
    }

    public function 
prefixValue($prefix) {
        return 
"{$prefix}ConcreteClass2";
    }
}
$class1 = new ConcreteClass1;$class1->printOut();
echo 
$class1->prefixValue('FOO_') ."\n";
$class2 = new ConcreteClass2;$class2->printOut();
echo 
$class2->prefixValue('FOO_') ."\n";?>

Monday 17 May 2010

How to create Singleton Class in PHP

<?php
class singleton
{
  
     /** Store the instance of the class */
     private static $instance;
    
     private function __construct()
     {
     }
    
     public static function getInstance()
     {
         //__CLASS__ is always resolved to this class, even if subclassed
         if (!self::$instance instanceof self)
         {
             self::$instance = new self();
         }
        
         return self::$instance;
     }
    
     public function __clone()
     {
        throw new Exception("Cloning of this object is not supported.");
     }
    
     public function someMethod()
     {
         echo "Doing something!";
     }

}
?>