Monday, September 19, 2016

Tools using for my current project

Tools I am using for my current project
•  React - https://facebook.github.io/react/: A Javascript library for building user interfaces
•  Cerebral - http://www.cerebraljs.com/ : A state controller with its own debugger
•  GraphQL - http://graphql.org/ : A query language for your API
•  TypeScript - https://www.typescriptlang.org/: JavaScript that scales.
•  EventStore - https://geteventstore.com/: open-source, functional database with Complex Event Processing in JavaScript
•  ECMAScript 2016 (ES2016): New version of Javascript
•  Webpack - https://webpack.github.io/: Module bundler
•  node.js - https://nodejs.org/ : JavaScript runtime built on Chrome's V8 JavaScript engine.

Tools using for my current project

Tools using for my current project
•  React - https://facebook.github.io/react/: A Javascript library for building user interfaces
•  Cerebral - http://www.cerebraljs.com/ : A state controller with its own debugger
•  GraphQL - http://graphql.org/ : A query language for your API
•  TypeScript - https://www.typescriptlang.org/: JavaScript that scales.
•  EventStore - https://geteventstore.com/: open-source, functional database with Complex Event Processing in JavaScript
•  ECMAScript 2016 (ES2016): New version of Javascript
•  Webpack - https://webpack.github.io/: Module bundler
•  node.js - https://nodejs.org/ : JavaScript runtime built on Chrome's V8 JavaScript engine.

Javascript Array Methods: Unshift(), Shift(), Push(), And Pop()

push() and pop() methods - work at the end of the array, where the index is largest.
unshift() and shift() - work on the beginning of the array, where the index is smallest.

Note: Unshift() and shift() are basically the same as push() and pop(), only, at the other end of the array.

Javascript Array: Push() Method
The push() method can append one or more elements to the end of an array. This alters the array on which the method was called.
// Build an array of test data.
var data = [ "X" ];

// Push data onto the array.
data.push( "A" );

/* Note that it can take more than one argument, each of which is individually appended to the array.
 In the output, notice that when push() takes multiple arguments  they are appended in a left-to-right order (mimicking their
appearance in the arguments list). */
data.push( "B", "C" );

// Output resultant array.
console.log( data );
When we run the above code, we get the following console output:
["X", "A", "B", "C"]
Javascript Array: Pop() Method
The pop() method pulls the last element off of the given array and returns it. This alters the array on which the method was called. If you call pop() on an empty array, it returns an undefined value.
// Build an array of test data.
var data = [ "A", "B", "C" ];

// Pop the element off of the end of the array.
console.log( data.pop() );
console.log( data );
When we run the above code, we get the following console output:
C
["A", "B"]
Javascript Array: Unshift() Method
The unshift() method is like the push() method, only it works at the beginning of the array. The unshift() method can prepend one or more elements to the beginning of an array. This alters the array on which the method was called.
// Build an array of test data.
var data = [ "X" ];

// Unshift data onto the array. 
data.unshift( "A" );

/* Note that it can take more than one argument. In the output, notice that when unshift() takes multiple arguments, they are prepended in a right-to-left order (mimicking their appearance in the arguments list). */
data.unshift( "B", "C" );

// Output resultant array.
console.log( data );
When we run the above code, we get the following console output:
["B", "C", "A", "X"]
Javascript Array: Shift() Method
The shift() method is like the pop() method, only it works at the beginning of the array. The shift() method pulls the first element off of the given array and returns it. This alters the array on which the method was called. If you call shift() on an empty array, it returns an undefined value.
// Build an array of test data.
var data = [ "A", "B", "C" ];

// Shift the element off of the beginning of the array.
console.log( data.shift() );
console.log( data );
When we run the above code, we get the following console output:
A
["B", "C"]

It used to be difficult for me to always keep it straight in my head which end of the array push() and pop() operated on (especially when reading unfamiliar code). But, now that I realize that unshift() and shift() are the same as push() and pop() only at the opposite end of the array, it certainly helps keep a certain mental order. I hope that this helps others at it has helped me.

Source: http://www.bennadel.com/blog/1796-javascript-array-methods-unshift-shift-push-and-pop.htm

Monday, May 23, 2016

How To Add Odd/Even Loop In Array Using PHP?

There are many ways to add odd/even loop in array using PHP. The most common and easy one I often use is the one below. With the help of following code, I can alternate the color of divs or tables or cell using odd and even classe.
$class = 'odd';  
for(.........) # no problem what kind of loop you are using(for,foreach,while,do)
{
   $class = ($class == 'even' ? 'odd' : 'even');
   #some code 
}

Ref: http://stackoverflow.com/a/15876816

Friday, May 20, 2016

Abstract, Final classes & methods and Interface in PHP

Abstract Class
Abstract classes are those classes which can not be directly initialized. Or in other word we can say that you can not create object of abstract classes. Abstract classes always created for inheritance purpose. You can only inherit abstract class in your child class.
Usually abstract class are also known as base class. We call it base class because abstract class are not the class which is available directly for creating object. It can only act as parent class of any normal class. You can use abstract class in class hierarchy. Mean one abstract class can inherit another abstract class also.

In abstract classes this is not necessary that every method should be abstract. Method in abstract class in php could be public, private, protected. You can create abstract classes in php using abstract keyword. Once you will make any class abstract in php you can not create object of that class.
abstract class abc
{
public function xyz()
{
return 1;
}
}
$a = new abc();//this will throw error in php
Above code will throw error in php.

Abstract classes in php are only for inheriting in other class.
abstract class testParent
{
public function abc()
{
//body of your funciton
}
}
class testChild extends testParent
{
public function xyz()
{
//body of your function
}
}
$a = new testChild();
In above example you are creating of testChild Class. TestChild class is inheriting testParent abstract class. So your abstract class is only available for inheritance. Main motive of creating abstract classes in php is to apply restriction of direct initialization or object creation.

Abstract method
As we know that abstract functions are those functions of abstract class which is only defined. It will be declared in your child class. You can create any method abstract using keyword abstract. You can only create abstract method either in abstract class or interface. Following is example of the abstract method implementation:
abstract class abc
{
abstract protected function f1($a , $b);
}
class xyz extends abc
{
protected function f1($name , $address)
{
echo "$name , $address";
}
}
$a = new xyz();
In class abc we have defined an abstract function f1. Now when we have inherited class abc then declared function f1. If you have an abstract method in your abstract class then once you inherit your abstract class then it is necessary to declare your abstract method. If you will not declare your abstract method then PHP will throw error in that case.

You can declare your abstract method in child class with the same visibility or less restricted visibility.
abstract class parentTest
{
abstract protected function f1();
abstract public function f2();
//abstract private function f3(); //this will trhow error
}
class childTest
{
public function f1()
{
//body of your function
}
public function f2()
{
//body of your function
}
protected function f3()
{
//body of your function
}
}
$a = new childTest();
In above code you can see that you have declare 3 function in abstract class. But private declaration of the abstract method will always throw error. Because private method is available only in the same class context. But in case of f1. This is protected. Now in child class we have defined it as public because public is less restricted than protected. And for function f2 which is already public so we have defined it as public in our child class. We have defined it public because no any visibility is less restricted than public.

Final class
A final class is a class that cannot be extended. To declare a class as final, you need to prefix the ‘class’ keyword with ‘final’. Example below.
final class BaseClass {
   public function myMethod() {
      echo "BaseClass method called";
   }
}
 
//this will cause Compile error
class DerivedClass extends BaseClass {
   public function myMethod() {
      echo "DerivedClass method called";
   }
}
 
$c = new DerivedClass();
$c->myMethod();
In the above example, BaseClass is declared as final and hence cannot be extended (inherited). DerivedClass tries to extend from BaseClass and hence the compiler will throw a compile error.

Final Method
A final method is a method that cannot be overridden. To declare a method as final, you need to prefix the function name with the ‘final’ keyword. Example below:
class BaseClass {
   final public function myMethod() {
      echo "BaseClass method called";
   }
}
 
class DerivedClass extends BaseClass {
   //this will cause Compile error
   public function myMethod() {
      echo "DerivedClass method called";
   }
}
 
$c = new DerivedClass();
$c->myMethod();
In the above example, DerivedClass extends from BaseClass. BaseClass has the method myMethod() declared as final and this cannot be overridden. In this case the compiler causes a compile error.

When to declare a class as final
You should declare a class as final when you think that you implementation of that class should not change in the derived class. You should do this mainly for Utility classes where you don’t want the behavior/implementation of your class to change.

When to declare a method as final
You should declare a class method as final when you think that the method you develop contains necessary functionality to support your application and any modification or change to the functionality can cause unexpected errors/bugs.

Interface in PHP
You can create interface in php using keyword interface. By implementation of interface in php class you are specifying set of the method which classes must implement. You can create interface in php using interface keyword. Rest of the things are typically identical to classes. Following is very small example of interface in php.
interface abc
{
public function xyz($b);
}
So in above code you are creating interface with name abc. Interface abc has function xyz. Whenever you will implement abc interface in your class then you have to create method with name xyz. If you will not create function xyz then it will throw error.

You can implement your interface in your class using implements keyword. Let us implement our interface abc in our class.
class test implements abc
{
public function xyz($b)
{
//your function body
}
}
You can only define method in interface with public accessibility. If you will use other than public visibility in interface then it will throw error. Also while defining method in your interface do not use abstract keyword in your methods.

You can also extend interface like class. You can extend interface in php using extends keyword.
interface template1
{
public function f1();
}
interface template2 extends template1
{
public function f2();
}
class abc implements template2
{
public function f1()
{
//Your function body
}
public function f2()
{
//your function body
}
}
So here template2 has all property of template2. So whenever you will implement template2 in your class, you have to create function of both interfaces.

You can also extend multiple interface in one interface in php.
interface template1
{
public function f1();
}
interface template2
{
public function f2();
}
interface template3 extends template1, template2
{
public function f3();
}
class test implements template3
{
public function f1()
{
//your function body
}
public function f2()
{
//your function body
}
public function f3()
{
//your function body
}
}

You can also implement more than one interface in php class.
interface template1
{
public function f1();
}
interface template2
{
public function f2();
}
class test implements template1, template2
{
public function f1()
{
//your function body
}
public function f2()
{
//your function body
}
}
You can not implement 2 interfaces if both share function with same name. It will throw error.

Your function parameter in class must be identical to the parameter in the interface signature. Following is example some example.
interface template1
{
public function f1($a)
}
class test implements template1
{
public function f1($a)
{
echo $a;
}
}

Above will work. But following example will not work:
interface template1
{
public function f1($a)
}
class test implements template1
{
public function f1()
{
echo $a;
}
}

But it is not necessary to use the same name of the variable. Like $a. You can also use any name. For example:
interface template1
{
public function f1($a)
}
class test implements template1
{
public function f1($name)
{
echo $name;
}
}

If you are using default argument then you can change your value of the argument. For example
interface template1
{
public function f1($a = 20)
}
class test implements template1
{
public function f1($name  = "ankur")
{
echo $name;
}
}


Source: http://www.techflirt.com/tutorials/oop-in-php/abstract-classes-interface.html
http://www.sunilb.com/php/php-5-tutorial-final-class-and-methods