Friday, June 26, 2015

Started to learn Yii 2

I am learning Yii 2 for last 5 days, but I still not get the point. I totally got confused and losing energy, time, confident etc.

I got followings:
1. After my long research I got the point of using "Composer". And learned how to install and use it.
2. Understand url routing.(Like naming controller and functions).
3. Under stand MVC structure which Yii using(Because I already used CodeIgniter).
4. Under stand DB Active Record Queries.

I not got followings:
1. The way of OOPS concept they using is totally making me unhappy. I know oops as middle level. But here they randomly calling function, properties using object OR class. In more case I can't find the function which they called. In that cases I decide that may be default or core.
2. And mainly I am not satisfied with the documents they given(http://www.yiiframework.com/doc-2.0/guide-README.html). They just explained the concept blindly, there is no fully run-able example codes. Just gave the syntax.
3. I cannot understand and implement the login authentication they explained.

As of now I am in 20% only, still I am trying. I hope will get the point in next week. I will post that exp on next week...

Friday, April 10, 2015

Assign value or array in PHP variable to Javascript variable.

<?php
$name = 'Einstein';
$categories = array('1'=>'Red', '2'=>'Green', '3'=>'Blue');
?>

<script type="text/javascript">
// Assign a PHP value/string to JS variable.
var name = '<?php echo $name; ?>';
console.log(name);                       // Einstein

// Assign a PHP array to JS variable.
var categories = JSON.parse('<?php echo json_encode($categories); ?>');
console.log(categories[3]);           // Blue
</script>

Thursday, July 24, 2014

Simple, best and standard way to make Thumbnail Images in PHP using Imagick

There is lot of way and plugins available to make thumbnail images in PHP. But that was the problem, because we are confused about which was the best one. After I research on these, I found using Imagick is the best one(Its only on my point of view.).

Imagick
Its an PHP extension to create or modify images. Imagick = Image + Magick. In old periods peoples wrote Magic as Magick. To install these run following command on your linux terminal.

pecl install imagick

After installing Imagick using above command, add “extension=imagick.so“ at the end on php.ini file which is located on /etc/php5/apache2/php.ini and restart the apache.

Example
<?php
$img_old_path = 'images/sun.jpg';     // Input image.
$img_new_path = 'images/thumbnail/sun.jpg';      // Output thumbnail image.

$img = new Imagick($img_old_path);

// Orientation operation start.
// If you remove this orientation block, there will no issue on generating thumbnail. But image may rotated if it has high difference in height and width.
$orientation = $img->getImageOrientation();
switch($orientation) {
    case imagick::ORIENTATION_BOTTOMRIGHT: 
        $img->rotateimage("#000", 180); // rotate 180 degrees.
    break;

    case imagick::ORIENTATION_RIGHTTOP:
        $img->rotateimage("#000", 90); // rotate 90 degrees CW.
    break;

    case imagick::ORIENTATION_LEFTBOTTOM: 
        $img->rotateimage("#000", -90); // rotate 90 degrees CCW.
    break;
}
$img->setImageOrientation(imagick::ORIENTATION_TOPLEFT);
// Orientation operation end.

$img->thumbnailImage(200 , 200, TRUE);
/* Max width and max height with best fit. Will return boolean;
This is the best method to make thumbnail. Action behind the scene is...
1. If given image width is larger than heigh then make thumbnail width as 200(here) and fix height according to that.

2. Else if image hight is larger than width then make thumbnail height as 200 and fix width according to that.*/

$img->writeImage($img_new_path); // Write the generated thumbnail. Will return boolean.
?>

References

Friday, July 18, 2014

Abstract class in PHP

Concept
Abstract class cannot be instantiated, that is we can not create a object. Any class which has at-least one abstract function should be  declared as abstract. These abstract functions are only can declare, can not define here. But these are must be defined in classes which are inherited this abstract class. We can declare this abstract function as any type(public, protected or private) in abstract class, but it should be same or less restricted type in inherited class(For example declared as protected may had public in derived class). And number of arguments also should be same(May had additional optional argument in derived class.).

Clear Example
<?php
abstract class A                          // Abstract class.
{
abstract protected function fun1();   // Declaring abstract function as protected.
abstract public function fun2($i);  // Declaring abstract function as public with one argument.
abstract public function fun3($i,$j); // Declaring abstract function as public with two arguments.

public function fun4()  // Declaring and defining a normal function in abstract class.
{
echo 'fun4 definition from class A(abstract class).<br />';
}
}

class B extends A
{
public function fun1()  // Definiion for a abstract function.
{
echo 'fun1 definition from class B.<br />';
}

public function fun2($i)  // Definiion for a abstract function with exact no.of arugument.
{
echo 'fun2 definition from class B.<br />';
}

public function fun3($i,$j,$k=30)  // Definiion for a abstract function with optional arugument.
{
echo 'fun3 definition from class B.<br />';
}
}

$b=new B();
echo $b->fun1();
echo $b->fun2(10);
echo $b->fun3(10,20);
echo $b->fun4();
?>

Abstract vs Interface
1. Abstract is a class but interface not. So memory allocation for abstract is high.
2. Abstract class can had normal functions and variables. But interface cannot.
3. Functions on interface must be public but in abstract class may had anything.
4. A class can inherit only one abstract class but can inherit more than one interface.

Thursday, July 17, 2014

Interface in PHP

Basic Concept
Interface is declared as same as class, except interface having only function declarations without any definition. A class which implementing a interface must be defined all functions which are declared in interface. We can implement a interface using the keyword implements.

Example 1: Simple Interface Concept.
<?php
interface BasicAction
{
        public function grow();
}

class Man implements BasicAction
{
        public function speak()
        {
               echo 'Will Speak';        
        }

        public function grow()
        {
               echo 'Will Grow';        
        }
}

$obj=new Man();
$obj->speak();
$obj->grow();
?>

Example 2: Extend a interface on another interface.

interface BasicAction
{
        public function grow();
}

interface Animal extends BasicAction
{
        public function walk();
}

class Deer implements Animal
{
        public function walk()
        {
               echo 'Will Walk';        
        }

        public function grow()
        {
               echo 'Will Grow';        
        }
}

Example 3: Multiple inheritance of interfaces on a class

interface A
{
        public function fun1();
}

interface B
{
        public function fun2();
}

class Test implements A, B
{
        public function fun1()
        {
               echo 'Fun 1 Definition';
        }

        public function fun2()
        {
               echo 'Fun 1 Definition';
        }
}

Interface Characters
1. All functions on interface must be public.
2. All functions on interface must be defined in classes which are implements interface.
3. We can inherit more than one interface on a class using comma(,) separator.
4. We can extend a interface on other interface.
5. We can declare constants inside a interface. Which will work as constants under class, except we cannot overwrite constants on other class or interface which implements or extends this interface.
6. Prior to PHP 5.3.9, a class could not implement two interfaces that specified a method with the same name, since it would cause ambiguity. More recent versions of PHP allow this as long as the duplicate methods have the same signature.

Wednesday, July 16, 2014

Final keyword in PHP

If we declare a function inside a class as final, we cannot use that function name in its derived classes. That is final keyword avoid method overriding. You can see more about method overriding here.

Example 1:
<?php
class A
{
     final function test()                // Declare function as final.
     {
          echo 'From base class';
     }
}

class B extends A
{
      function test()                      // Will throw an error.
      {
            echo 'This never run.';
      }
}
?>

And we can also avoid extending a class by declaring a class as final.

Example 2:
final class A                               // Declare class as final.
{
     function fun()
     {
     }
}

class B extends A                      // Will throw an error.
{
}

And note we can only declare function and class as final, cannot declare variable as final.

Constant variables in PHP

Basic Concept
By declaring a variable as constant, it will available all over the program and only we can access it, cannot modify it.

In PHP we can declare a constant variable in two ways. That are using define() function and const keywords.

Before going to see deep difference between these, first look a simple program using these two methods.

Example 1:
<?php
define('PIE',3.143);
const SHAPE='Circle';

function fun()
{
       echo PIE.'<br />';
       echo SHAPE;
}

fun();
?>

Output:
3.143
Circle
?>

On above example there no difference between using define() and const. But it has following notable differences.

1. const execute on compile time and define() execute on run time. That is we cannot declare const under a function or and conditional statement like "if".

2. We cannot declare define() in class, but can declare const.

See the below complex example which is using define() and const in all possible manners.

Example 2:
<?php
define('REGISTER_NO',90323540126);  // Declaring a constant using define keyword in global.
const NAME = 'Ramasamy Kasi';          // Declaring a constant using const keyword in global.
const ROLL_NO = 35;

function fun()
{
define('DEPARTMENT','Computer Science'); // Declaring a constant using define inside a funtion.
// const TOTAL = 450;    // Try to declare a constant using const inside a funtion. ITS WRONG.

echo REGISTER_NO.'<br />'; // Accessing consatants inside a function.
echo NAME.'<br />';
}

class Test
{
// define('AVERAGE',90) // Try to declare a constant using define inside a class. ITS WRONG.
const SEMESTER = 1;          // Declaring a constant using const inside a class.

function sample()
{
echo self::SEMESTER.'<br />';          // WAY 1: Accessing class constant.
}
}

fun(); // Calling function.
echo ROLL_NO.'<br />'; // Accessing constant, which is created in side a function.
echo DEPARTMENT.'<br />'; // Accessing constant, which is created on global.
$obj=new Test();
$obj->sample(); // Calling class function.
echo TEST::SEMESTER; // WAY 2: Accessing class constant.
?>

Output:
90323540126
Ramasamy Kasi
35
Computer Science
1
1

Learn JavaScript - String and its methods - 16

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>String and it's methods - JS...