Wednesday, December 9, 2015

Unit Testing AngularJS Directives With External Templates

Install node.js in ubuntun 14.04

Monday, November 2, 2015

Dependency Injection in PHP

Introduction

Dependency Injection is an object oriented design pattern that you come across when extending application frameworks logic. It has to do with "more than one class (object) depending on the services of another".
There are lots of articles, tutorials, explanations on the Dependency Injection design pattern. They also mention Inversion of Control. It is based on the popularly known Hollywood catch phrase, "Don't call us, we'll call you".
Those articles often have two things in common: its either their base of explanation on a particular technology (such as Java or C#) or they delve into highly detailed concepts without explaining the main concepts behind the dependency design principle.
This article aims to bridge those gaps by explaining the main concepts behind Dependency Injection (Inversion of Control) design pattern, explain its advantages when compared with other alternatives and list the challenges that you may be faced when using it to write applications.

How Dependency Injection Works?

In normal everyday object oriented application development, a dependency object is passed to a dependent object directly. This may result in lots of issues such as tight coupling, less flexible and reusable code, among others.
Dependency Injection and Inversion of Control Containers are meant to solve these problems by making the dependent object (client) call a dependency when needed. This makes it very easy to develop robust, independent and reusable object oriented class services.

Dependency Injection in Practice

Let me illustrate these concepts with a quick example of a car class object that is dependent on an automobile class.
<?php

 class automobile
 {
  public function __construct()
  {
   echo "I'm the Automobile Class";
  }

  public function doSomething()
  {
   echo "I'm doing something";
   return;
  }
 }

?>
The snippet above defines the automobile class which would act as a parent to the car class. Normally, the car class object would be defined this way:
<?php

class testingDI
{

 // a variable for holding a Service Object
 private $di_service;

 public function __construct() { }

 // a Service object to be passed in as parameter
 public function setService( Service di_service)
 {
  // Setting the global di_service object
  // equal to the passed in Service object
  $this->di_service = di_service;
 }
}

?>
Similarly, in interface injection, the client (dependent object) implements an interface which defines how a dependency should be accepted and set. An example is illustrated below:
<?php

 interface DIInjector
 {
  public function diInjector( Service di_service);
 }

 class testingDI implements DIInjector
 {
  // a variable for holding a Service Object
  private $di_service;

  // a Service object to be passed in as parameter
  public function setService( Service di_service)
  {
   // Setting the global di_service object
   // equal to the passed in Service object
   $this->di_service = di_service;
  }
 }

?>
Dependency Injection helps to create software that follows the dependency inversion principle. The Dependency Inversion Principle is a common way, especially with object oriented designs, of decoupling various part of a software module into individual entities.
Following this principle means that the Client (the dependent object) does not need to know how to construct it's dependencies, it's the injecting code that constructs the services (the dependencies) and injects them into the client

Dependency Injection and the Single Responsability Principle

This is very similar to the Single Responsibility Principle that states that every object (a class in this case) should have responsibility over a single part of the functionality provided by the software. In order words, the Single Responsibility Principle is all about the simple concept of One Class, One Task.

Conclusion

Dependency Injection is a software design pattern that helps to provide a better structure to an object oriented software design. Although, there are more advanced alternatives to pure Dependency Injection such as using a Dependency Injection (Inversion of Control) Container, instead of directly using the Dependency Injection approach, it still is a major building block for most of this concepts.
In the next article, we look further into the various ways of implementing Dependency Injection. We would talk more about the Controller Injection, Setter Injection and Interface Injection approaches and how they can be useful in various scenarios.
We will also talk about IoC Containers and how to build one for better software reusability, maintainability and testability.
If you liked this article or you have a question about the presented dependency injection concepts, post a comment here.

Thursday, May 21, 2015

Tuesday, May 19, 2015

PHP Security Cheat Sheet

PHP The Right Way: The Book

Making API Calls in AngularJS using Angular’s $http service

jQuery setTimeout() Function Examples

Using JavaScript to Create Geospatial and Advanced Maps

The 15 Best JavaScript Charting Libraries

Tuesday, April 28, 2015

PHP: My favourite new features in PHP 5.6 / 5.5 / 5.4 / 5.3

PHP 5.6 Features

From PHP 5.5 to 5.6
----------------------------------------
http://php.net/manual/en/migration56.new-features.php
1. Constant expressions
   It is also now possible to define a constant array using the const keyword:
2. Variadic functions via ...
3. Argument unpacking via ...
4. Exponentiation via **
5. use function and use const
6. phpdbg
7. Default character encoding
8. php://input is reusable
9. Large file uploads
10. GMP supports operator overloading
11. hash_equals() for timing attack safe string comparison
12. __debugInfo() magic method has been added
13. gost-crypto hash algorithm
14. SSL/TLS improvements
15. pgsql async support

PHP 5.5 Features

From PHP 5.4 to 5.5
----------------------------------------
http://php.net/manual/en/migration55.new-features.php

http://www.techrepublic.com/blog/software-engineer/10-improvements-in-php-550-for-web-developers/
1. Generators added
2. finally keyword added
3. New password hashing API
4. foreach now supports list()
5. empty() supports arbitrary expressions
6. array and string literal dereferencing
7. Class name resolution via ::class
8. OPcache extension added
9. Apache 2.4 handler supported on Windows
10. Improvements to GD

Friday, March 20, 2015

URL Routing in PHP


How to Implement URL Routing in PHP

Among the major aspects observed by search engines to determine the relevance of a particular page to a specific search term is that whether the URL link itself embraces a certain word or not. The instant ability to define your URLs in a more human readable, SEO friendly format and to remap old URLs to novel functionality is the key reason for organizations to adopt for URL routing.

What is URL Routing?
URL Routing is a mechanism used to map URLs to the code that gets executed only when a certain request is received at the server.
Why you need URL Routing?
URL Routing makes your URLs considerably more meaningful. Suppose, you have a page that searches for books based on the author’s name. It may appear like this:
index.php?action=search&type=book&author=fitzgerald
Such type of URLs are difficult to understand and remember for the user. Therefore, to make these URLs more user friendly, you use ‘URL Routing’ and transform these URLs into simpler format.
/search/book/ftizgerald
Another great advantage of URL routing is found in Search Engine Optimization and there is no denying to the fact that strong SEO can enhance the positioning of your website and eventually profitability.
How to implement URL Routing in PHP?
The following guide will help you in implementing URL routing in PHP and Apache.
  1. First of all, make sure you have installed and enabled rewrite module in Apache’s configuration
  2. Now, we have to create a .htaccess file in root directory of our website
    1
    2
    3
    4
    
        Options -MultiViews
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^ index.php [QSA,L]
    * Check, if the mod_rewrite has been enabled or not. If, yes then the following configuration will be used.
    * ‘RewriteEngine On’ enables the rewrite engine. Next, we will handle all the requests that do not correspond to the file names. After that, we will pass the requests to index.php file. Now, index.php will manage all the requests.
  3. For every page request, index.php will be executed. In order to distinguish between the different requests, you will have to use $_SERVER['REQUEST_URI']
  4. Next, simply explode $_SERVER['REQUEST_URI'] with ‘/’ and you will get the substrings that will help you to compare and execute the code corresponding to them.
The Real Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
 /*
 The following function will strip the script name from URL i.e.  http://www.something.com/search/book/fitzgerald will become /search/book/fitzgerald
 */
 function getCurrentUri()
 {
  $basepath = implode('/', array_slice(explode('/', $_SERVER['SCRIPT_NAME']), 0, -1)) . '/';
  $uri = substr($_SERVER['REQUEST_URI'], strlen($basepath));
  if (strstr($uri, '?')) $uri = substr($uri, 0, strpos($uri, '?'));
  $uri = '/' . trim($uri, '/');
  return $uri;
 }
 
 $base_url = getCurrentUri();
 $routes = array();
 $routes = explode('/', $base_url);
 foreach($routes as $route)
 {
  if(trim($route) != '')
   array_push($routes, $route);
 }
 
 /*
 Now, $routes will contain all the routes. $routes[0] will correspond to first route. For e.g. in above example $routes[0] is search, $routes[1] is book and $routes[2] is fitzgerald
 */
 
 if($routes[0] == “search”)
 {
  if($routes[1] == “book”)
  {
   searchBooksBy($routes[2]);
  }
 }
Conclusion
This is how you will be able to implement minimal URL routing mechanism. There are a plenty of PHP frameworks that provide enhanced URL Routing features and you can try them as well.

Tuesday, February 24, 2015

How To Become A Successful Software Developer: 13 Steps

http://www.techgig.com/tech-news/editors-pick/How-To-Become-A-Successful-Software-Developer-13-Steps-29277?mailer_id=2181&utm_source=Mailer&utm_medium=TG_batch&utm_campaign=digest_news_2015-02-23&email=ndlganeswararao@gmail.com&activity_name=Mjk4NzA=&template_type=3&dt=&auto_login=bmRsZ2FuZXN3YXJhcmFvQGdtYWlsLmNvbUAjJEAjJDUzNDM1M0AjJEAjJDEzODQ0NjE2MTE=&src_type=autoLogin

or

Software Development is no longer considered as an art. But it was made as an engineering practice. A successful software developer is a person who is able to write each line of code with passion. And passion cannot be obtained by any software engineering degree, it must come from within yourself.

1.Be passionate about the being a software developer. If you are not passionate for sitting in front of the PC for the whole day, simply enjoying the lines of code you write; this is not for you.
 
2.Always start with the simple hello world.

3.Don't try to learn all the programming languages. What you need to know is the concepts.

4.Don't start programming with a languages such as C/C++. Try something like Java or C Sharp.

5.When you face an error. Always tell to yourself, "I'm NOT ALONE, Someone in this world should have faced the same error before". And GOOGLE the exact error message. Chances are 99% you will find the answer.

6.Be more public. If you will not show others that you are cool and you deserve more, how will they know? There is simple way to do that - start blogging, ask and answer questions, ensure Google knows something about you. Share your knowledge in your team and project. If you learned something new, why do not share it. You will forget it if will not be trying it.

7.Never use Microsoft notepad to do coding. Use at least Notepad++.

8.When you see an application(web application or windows), tell yourself. If this is an application. Then it's technically possible for me to build a similar one.

9.When you see an application(web application or windows), try to model it in your mind. Always ask yourself, can you build a similar application yourself. If not try to find the place where you think you might get stuck. And try to Google and find how to do it.

10.If you get stuck in a code logic for more than two hours; STOP your work; go out to have a tea or coffee, before trying it again.

11.Be confident. Help others and ask from others. Different people will have different aspects to solve an issue.

12.Never be ashamed, when you find the bug as a single line of code. Usually most of the bugs can be corrected in a single line of code. What matters is, how hard you tried in different aspects to solve the issue.

13.Find a mentor. This does not mean that you a need a person who will help you in doing your job - it is a coach or a more senior staff then you are. This means that you need a person who stays where you want to be and you need to take a leaf from his/her book. And if he will not be high enough - you will need to find another. But all the time you need to have that person. Also have people, friends if you will, who will help you keep on track. It could be your wife or girlfriend , or best friend who always supports you (in my case he is a developer, but this doesn't matter).