Monday, 17 April 2017

Difference between self and $this in php

$this
Use $this to refer to the current object. 

self::
Use self to refer to the current class. 


In other words, use  $this->member for non-static members, 
use self::$member for static members.

How we can Access abstract class function in child class

 Access abstract class function in child class

<?php

abstract class parentTest
{
abstract protected function f1();
abstract public function f2();
public function f4()
{
echo "Parent";
}
}

// Child class ...........
class childTest extends parentTest
{
public function f1()
{
echo "f1";
//body of your function
}
public function f2()
{
parent::f4();//Accessing parent class function
echo ":f2";
//body of your function
}
protected function f3()
{
echo "f3";
        //body of your function
}
}
$a = new childTest();
$a->f2();//Parent:f2

?>

Overloading and Overriding in php

Overloading in PHP


class overloading {
    public $methodName;
    public function __construct($argu) {
        $this->methodName = $argu;
    }
    public function __call($methodname, $agru) {
         if($methodname == 'sum2') {

          if(count($agru) == 2) {
              $this->sum($agru[0], $agru[1]);
          }
          if(count($agrument) == 3) {

              echo $this->sum1($agru[0], $agru[1], $agru[2]);
          }
        }
    }
    public function sum($a, $b) {
        return $a + $b;
    }
    public function sum1($a,$b,$c) {

        return $a + $b + $c;
    }
}
$object = new overloading('Sum');
echo $object->sum2(1,2,3);//6

Overriding in PHP

<?php

class A {
   function print() {
      return "print::1";
   }
}

class B extends A {
   function print() {
      return "print::2";
   }
}

$foo = new A;
$bar = new B;
echo($foo->print()); //"print::1"
echo($bar->print()); //"print::2"
?>

Tuesday, 11 April 2017

How to load jQuery Library when CDN fails

How to load jQuery Library when CDN fails
It is a good approach to always use CDN but sometimes what if the CDN is down (rare possibility though) but you never know in this world as anything can happen. Server may have reason to get down, so we need to be take care of these problems.

Below given jQuery code checks whether jQuery is loaded from Google CDN or not, if not then it references the jQuery.js file from your folder.


if (typeof jQuery == 'undefined'{
console.log(" Jquery undefined");
  document.write(unescape("%3Cscript src='MyScripts/jquery.1.12.1.min.js' type='text/javascript'%3E%3C/script%3E"));
}



It first loads the jQuery from Google CDN and then check the jQuery object. If jQuery is not loaded successfully then it will references the jQuery.js file from hard drive location. In this example, the jQuery.js is loaded from Scripts folder.