Ярлыки

.htaccess (4) тестирование (8) шаблоны проектирования (3) css (5) Debian (6) docker (2) Doctrine2 (6) Git (6) html (4) java (6) javascript (13) jquery (11) LFS (3) linux (23) mac os (4) mod_rewrite (2) MSSQL (4) MySQL (18) ORM Doctrine (17) patterns (3) PDO (3) perl (7) PHP (64) PHPUnit (8) Python (15) SEO (2) Silex (1) SimpleXML (1) SQL (14) ssh (4) Ubuntu (24) Yii1 (1) Zend Framework (19) ZendFramework2 (8)

суббота, 26 марта 2016 г.

Javascript. Inheritance.

/************************** Parent ***************************/
// constructor
function Animal(name) {
 this.name = name;
  this.speed = 0;
}

// parent methods in prototype
Animal.prototype = {
 run: function (speed) {
   this.speed += speed;
    console.log(this.name + " runs with speed " + this.speed);
    return this;
  },
  stop: function () {
   this.speed = 0;
    console.log(this.name + " stopped");
    return this;
  }
}
/************************** Parent ***************************/
/************************** Child ****************************/
// constructor
function Rabbit(name) {
 Animal.apply(this, arguments); // apply parent's constructor
}

// inheritance
Rabbit.prototype = Object.create(Animal.prototype);
// http://stackoverflow.com/questions/8453887/why-is-it-necessary-to-set-the-prototype-constructor
Rabbit.prototype.constructor = Rabbit;

// methods of child
Rabbit.prototype.jump = function () {
 this.speed++;
  console.log(this.name + " jumps");
  return this;
};

// method overriding
Rabbit.prototype.run = function (speed) {
 Animal.prototype.run.apply(this, arguments);
  this.jump();
  return this;
};
/************************** Child ****************************/

var rabbit = new Rabbit("Jack").run(10).stop();

console.log(rabbit instanceof Animal);
console.log(rabbit instanceof Rabbit);

вторник, 22 марта 2016 г.

PHP. Handle file_get_contents errors as Exception.

Below is the way to handle "file_get_contents" errors as ErrorExceptions:
set_error_handler(
    function ($errno, $errstr, $errfile, $errline) {
        throw new \ErrorException(
            $errstr, 0, $errno, $errfile, $errline
        );
    }
);

$contents = file_get_contents(
    $uri, false, $context
);

restore_error_handler();