Getting started with PHP on Azure

Hello Everyone, today let’s make ourselves ready to learn a new topic i.e. hosting PHP application on Azure. Many of us must have came across the word Azure and some of us must be knowing how to host the application written in PHP on it. Today we will go over how to host the application on Azure and how to auto-deploy the code through bitbucket. So, why to wait !! Lets start then :).

In this blog, we will mainly come across these following words.
1. PHP
2. Microsoft Azure
3. Bitbucket

1. PHP :-

“PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language.” – wiki

Chicago Manual of Style, content style guide
The word PHP i.e. Hypertext Preprocessor earlier called as Personal Home Page. In 1994, Rasmus Lerdorf created this pretty language and in 1995, it came into existence as an open source technology. Because of its simplicity (as most of the syntax resembles to C, C++, Java), it is much popular in web development market and among the developers. One can take 100% advantage of using this language to make dynamic web pages quickly. It is a server side scripting language. Server side, as the name suggests, it runs on a web server and it requires a web browser to send/receive request/response. Scripting language, as the name suggests, it is only compiled when an interpreter reads it and convert it into machine understandable language.

But, first and foremost thing why developers love this technology is easy to learn, free of cost, platform independent and secure.

2. Microsoft Azure :-

“Microsoft Azure is a cloud computing platform and infrastructure created by Microsoft for building, deploying, and managing applications and services through a global network of Microsoft-managed data centers.” – wiki

Chicago Manual of Style, content style guide
The name Azure suggests, bright blue in colour like a cloudless sky. But in our context, this is little different :D. It is a platform which provides PaaS (Platform as a Service) and IaaS (Infrastructure as a Service) categories of cloud computing services. It supports many different programming languages including Microsoft and non-Microsoft related technologies. Azure was launched on 1st February 2010 as Windows Azure and then acquired the name as Microsoft Azure on 25th March 2014.

It is easy to publish and manage websites with PaaS environment. Mostly used for cloud computing, which literally means of using network to manage data, process and store which is hosted on remote servers rather than a local or personal server.

3. Bitbucket :-

“Bitbucket is a web-based hosting service for projects that use either the Mercurial (since launch) or Git (since October 2011) revision control systems.” – wiki

Chicago Manual of Style, content style guide
I hope everyone here are familiar with GIT, the version control system which is used for data integrity and support for distributed workflows. It is used to manage source code and the name satisfies as SCM, Source Code Management.

Bitbucket is the solution for professional teams who uses GIT. It offers hosting source code in free of cost and it has the option to add wikis for project & option to track issues. It is similar to Github which is used to manage the code for a distributed application.

Now, let’s start with setting up and host a PHP application on Azure.

Step 1 :- First, we need to login into the account on Azure Portal. For that, we need to provide work or school or personal Microsoft account, otherwise need to create a new Microsoft account without any cost.

Once you successfully logged in into the system, it will redirect into Azure Portal page. See the below image for reference.

Screen Shot 2016-05-14 at 10.18.31 PM

Step 2 :- Now, click on All Resources -> New button for creating the base/marketplace.

Screen Shot 2016-05-14 at 11.22.49 PM

Then select Web App + MySQL for hosting the application. Click on Create button for creating the hosting setup.

Screen Shot 2016-05-14 at 11.24.06 PM

Step 3 :- Once you click on Create button, it will open one form to provide App Name, Subscription Details, Resource Group, Database Details and Legal Terms to accept. Provide a unique App Name as the website hosting name would be app_name.azurewebsites.net and the DB details (choose Database pricing tier as Mercury, the free one) as like below.

screen-shot-2016-05-14-at-11-25-18-pm

screen-shot-2016-05-14-at-11-27-15-pm

screen-shot-2016-05-15-at-1-01-44-am1

Step 4 :- Once you provide every information mentioned above, check Pin to dashboard option and click on Create. Now, it will start the process of deployment the server and it may take few time to complete the process.

submitting_deployment

Screen Shot 2016-05-16 at 12.33.44 PM

Step 5 :- Now lets go to Bitbucket (https://bitbucket.org/) and create the account. Do the Signup here (https://bitbucket.org/account/signup/) and you can see the Dashboard like the below once you successfully logged in into the system.

screen-shot-2016-05-16-at-12-45-25-pm

Step 6 :- Inside Bitbucket, go to Repositories -> Create Repository. Provide the necessary details there like below :
Screen Shot 2016-05-16 at 12.53.14 PM

Once you Create Repository, it will show the dashboard for Repository setup and then select “I’m starting from scratch”.

screen-shot-2016-05-16-at-12-57-01-pm

Step 7 :- Open your Terminal to run some commands. Follow the rules mentioned in the above section i.e. “Set up your local directory”, “Create your first file, commit, and push”. For reference, do the following :

Set up Git on your machine if you haven’t already

Create your first file, commit, and push

  • phpinfo() >> phpinfo.php
  • git add phpinfo.php
  • git commit -m ‘Initial commit with phpinfo’
  • git push -u origin master

Step 8 :- Now time is to connect Azure with the deployment source for continuous integration. Again lets go to https://portal.azure.com and click on All Resources. Select the app we have created so far i.e. test-demo-app (type as App Service) and then choose Deployment Source from PUBLISHING section like below.

screen-shot-2016-05-16-at-1-23-33-pm

Step 9 :-

Click on Choose Source and select Bitbucket as the source. Provide the Authorization details of Bitbucket. Once Authorization is completed, it will ask to choose Project and branch. Provide all the details like below.

Screen Shot 2016-05-16 at 1.32.54 PM

Step 10 :- After providing the details and click on OK will start the source deployment and finally it will show one flash message as Successfully set up deployment source.

We are all DONE !!! ๐Ÿ™‚ Now browse http://test-demo-app.azurewebsites.net/ (as we have provided the name as test-demo-app so far) and you can view the current PHP information for your server now.

Isn’t it cool & easy !!! ๐Ÿ™‚ So, why to wait? Create one simple PHP page and host the application into Azure along with continuous integration using git Bitbucket. Do you really find it useful? If yes, then please like and add some comments, if you want.

Thanks and I will be happy to hear from you ๐Ÿ™‚ :).

A walk through into Late Static Binding in PHP

Hello everyone, do we remember the concept of Static Binding and Dynamic Binding, the one we studied in Java classes during our college time?

Yes!! Then very nice :). Today in this blog, we will go through those concepts using PHP for better understanding and visibility for the persons like me :D.

What is Binding?
In simple software language, “connecting a method call, to the method body is known as binding”.

 

Types of Binding
There are two types of binding

  1. Static binding (early binding)
  2. Dynamic binding (late binding)

 

Example of static binding in PHP
Here in this case, class association is made during compile time.

<?php
class Vehicle {
    public function color() {
        echo 'car color is RED';
    }
}

$vehicle = new Vehicle();
$vehicle->color();
?>

This would print the output as : car color is RED

 

Example of dynamic binding in PHP
Here in this case, class association is not made until the object is created at execution time.

<?php
class Vehicle {
    public function color() {
        echo 'car color is RED';
    }
}

class Honda extends Vehicle {
    public function color() {
        echo 'car color is BLUE';
    }
}

$vehicle = new Honda();
$vehicle->color();
?>

This would print the output as : car color is BLUE

 

But, have you heard of another binding called “Late Static Binding”!! No…. then let’s go through that ๐Ÿ™‚ with some interesting example.

What is Late Static Binding?
PHP 5.3 implemented a new feature called Late Static Binding, which is used to reference the called class regarding to static method inheritance.

 

Why the name Late Static Binding?
It is the combination of two concepts i.e. Late Binding and Static Binding.
Late binding comes from the fact that static:: keyword will not be resolved using the class where the method is defined but it will rather be computed using runtime information.
Static binding comes from the fact that it can be used for (but is not limited to) static method calls.

 

Why it came into the picture?
To overcome the use of self keyword, Late static binding concept came. self does not follow the rules of inheritance and it always resolves to the class in which it is used. For example – if you make a method in a parent class and call it from a child class, self will always reference the parent, instead of child.

But in case of Late Static Binding, static keyword has been used extensively to represent the class where it is first used i.e. it binds to the runtime class.

 

Example of late static binding in PHP with self, parent and static

<?php

class Mango {       
    function classname(){
        return __CLASS__;
    }
    
    function selfname(){
        return self::classname();
    }
    
    function staticname(){
        return static::classname();
    }
}

class Orange extends Mango {     
    function parentname(){
        return parent::classname();
    }
    
    function classname(){
        return __CLASS__;
    }
}

class Apple extends Orange {     
    function parentname(){
        return parent::classname();
    }
    
    function classname(){
        return __CLASS__;
    }
}

$apple = new Apple();
echo $apple->selfname() . '<br/>';
echo $apple->parentname() . '<br/>';
echo $apple->staticname();

?>

This would print the output as :
Mango
Orange
Apple

 

Example of late static binding in PHP with forward_static_call()

<?php

class Mango
{
    const NAME = 'Mango is';
    public static function fruit() {
        $args = func_get_args();
        echo static::NAME, " " . join(' ', $args) . "<br/>";
    }
}

class Orange extends Mango
{
    const NAME = 'Orange is';

    public static function fruit() {
        echo self::NAME, "<br/>";
        
        forward_static_call(array('Mango', 'fruit'), 'my', 'favorite', 'fruit');
        forward_static_call('fruit', 'my', 'father\'s', 'favorite', 'fruit');
    }
}

Orange::fruit('NO');

function fruit() {
    $args = func_get_args();
    echo "Apple is " . join(' ', $args);
}

?>

This would print the output as :
Orange is
Orange is my favorite fruit
Apple is my father’s favorite fruit

 

Example of late static binding in PHP with get_called_class()

<?php

class Mango {
    static public function fruit() {
        echo get_called_class() . "<br/>";
    }
}

class Orange extends Mango {
    //
}

Mango::fruit();
Orange::fruit();

?>

This would print the output as :
Mango
Orange

So, did you get some idea of method overriding or binding? Do you really find it useful? ๐Ÿ™‚ If yes, then please like and add some comments, if you want.

Thanks ๐Ÿ™‚ and will be happy to listen from you ๐Ÿ™‚ :).

Install Zend Framework 2 on Windows OS !!!

As many of us know about ZF2 (Zend Framework 2), it is an open source, object oriented Framework designed for developing web applications and services using PHP 5.3+.

So, in this post we will go through the installation process for ZF2 Framework with a skeleton application so that it will be easier for the developer to continue developing the application directly :).

We will go through just simple 4 steps and then we will be done with the installation.

Step 1 :-
We can install ZF2 skeleton application in 2 different ways.

Case 1 :-

Go to here and click the zip button to download. Unzip this file into the directory where you keep all your vhosts (in this case, I used D:/projects/) and rename the folder to ZendSkeletonApplication.

Case 2:

Install GIT Bash if you have not installed yet. Installing GIT is easy to do. Go to http://git-scm.com โžก Downloads โžก Select OS โžก Save File โžก Run โžก Accept all and install it.

Open GIT bash and open the folder where you keep all your vhosts or where you want to create the Project (in this case, I used D:/projects/) and run this command –

git clone git://github.com/zendframework/ZendSkeletonApplication.git

After this, one folder will be created on your specified path named as ZendSkeletonApplication.

Step 2 :-
Open GIT Bash and go to ZendSkeletonApplication folder using

cd D:/projects/ZendSkeletonApplication

Then run the following command

E:/xampp/php/php.exe composer.phar self-update

Then after executing the above command, run the below command

E:/xampp/php/php.exe composer.phar install

Note: Here in my case, I have my xampp folder inside E:/ drive.

Step 3 :-
Add the following in httpd-vhosts.conf (E:\xampp\apache\conf\extra\httpd-vhosts.conf) file. After adding the below lines, restart your xampp.

<VirtualHost *:80>
    ServerName http://zend.localhost.com
    ServerAlias zend.localhost.com
    DocumentRoot "D:\projects\ZendSkeletonApplication\public"
    SetEnv APPLICATION_ENV "development"
    <Directory "D:\projects\ZendSkeletonApplication\public">
        DirectoryIndex index.php
        AllowOverride All
        Order allow,deny
        Allow from all
    </Directory>
</VirtualHost>

Step 4 :-
Finally, add the following in your host file (C:\Windows\System32\drivers\etc\host).

zend.localhost.com

๐Ÿ˜Ž great !!! Now, everything is set and ready to use. Browse http://zend.localhost.com and you will get the default skeleton application page :eek:.

Screen Shot 2014-10-06 at 6.47.59 pm

If you are finding the page like above, while browsing http://zend.localhost.com, then ๐Ÿ˜Ž :D, we are done now (clap).

Thanks ๐Ÿ™‚ and will be happy to listen from you ๐Ÿ™‚ :).

Why to use Framework in PHP!!

Introduction:-
The word FRAMEWORK is the combination of two words i.e. FRAME and WORK. It means, one FRAME has already been designed and developer has to WORK on that FRAME to meet his/her project requirements. It’s just a tool, which helps developer to code better and faster.

In computer language, a Framework is an universal, reusable software platform to develop software applications, products and solutions. In other words, we can say it is some kind of library, a piece of software, which provide web developers with code base and consistent standardized ways of creating web applications.

Back when PHP and other technology started, developers were writing their own custom code for each functionality. After then, some people realized that, writing the same code every time, in every page, not reusing it extensively, plain PHP is not so effective.

Afterwards, as the PHP object model developed (especially with the launch of PHP 5), framework development really came into action and became so popular.

So, lets not stretch this topic as chew gum ๐Ÿ˜€ and let’s check what are the key benefits of using a Framework, especially in PHP.

Organizing Code and File :-
When you setup a PHP Framework, it already has a certain folder structure. Most of the Framework, use MVC (Model-View-Controller) guideline or structure for managing the code. MVC pattern allows to separate business logics and presentation layer of your website, making its structure consistent and maintainable. It is expected from you to follow the same standards and keep everything organized in a certain way. Once you get used to this model, you will never want to go back.

The MVC Pattern :-
The Popular PHP frameworks like CakePHP, CodeIgniter, Zend Framework , Symfony follows Model View Controller(MVC) design pattern which separates business logic from user interface making the code cleaner and extensible. The way the MVC Pattern applies to PHP applications:

  • Models represent your data structures, usually by interfacing with the database.
  • Views contain page templates and output.
  • Controllers handle page requests and bind everything together.

Enforcing of Good Coding Standards :-
Another effect of using Framework is maintaining coding standard in an efficient manner. Coding conventions are a set of guidelines for a specific programming language that recommend programming style, practices and methods for each aspect of a piece program written in this language. Using Framework makes it so much easier to code as you should.

Utilities and Libraries :-
PHP is a great language for web development and provides countless number of tools and libraries. As everybody can guess, these frameworks contains a lot of libraries to make your job easier. All top PHP frameworks come with certain Libraries and Helpers, that help you with:

  • Form Validation
  • SOAP/REST API
  • Caching Mechanism
  • Input/Output Filtering
  • Data Handle With JSON
  • Database Abstraction
  • Session and Cookie Handling
  • Email, Calendar, Pagination etcโ€ฆ

Less Code & Faster Development :-
These MVC framework will really helps you to develop the project rapidly, if you know one framework well then youโ€™ll never worry about the project deadline. You will write less code, which means less time spent typing. You will not have to chase down 3rd party libraries all the time for every new project because most of them will come with the default framework install. While working on a large project in a team, these MVC PHP framework will be a great tool for development as you can assign a developer to develop MVC part of a module for a developer and integration will be quite easy of these developed module at final level. Also, since you are being more organized, it will be much faster to chase down bugs, maintain code, and make changes to existing code.

Community Support :-
All popular PHP Frameworks have great active communities behind them. There is no lack of support from the community, the documentation, and the developers. If you have any questions or need any clarifications, a quick search with the right keywords should almost always give you relevant results. If not, thereโ€™s still the message boards, mailing Lists, the official and unofficial forums or the unofficial IRC channel.

Security :-
With a framework, most of the work for code security can be done for you automatically. For example:

  • Central Authentication
  • Any value passed to database object gets filtered against SQL injection attacks
  • Central input validation and filtering
  • All html generating functions, such as form helpers and url helpers filter the output automatically
  • Cross Site Request Forgery (CSRF) Protection
  • Session Management Security
  • Cross Site Scripting (XSS) Protection
  • Encrypting cookies automatically is only a matter of changing a config option and lot moreโ€ฆ

Performance Tools :-
Framework tools are utility module for maintaining modular application. You can actually gain perCode generation make developers life easier by creating files and default content automatically so you donโ€™t have to do the same thing again. Framework come with tools that help you to do caching, benchmarks, profiling etcโ€ฆ Modern frameworks are also great with dynamic loading, as they load code only as needed. Lazy loading concept is also there, to make the web page ready, till the time content is ready. Different page requests can load different amount of library code based on what needs to be used.

Simplified and Pretty URLs :-
Most Framework use a bit of mod_rewrite magic to ensure, your URLS are always clean and accessible. Apache handles requests to a particular URL, and (hidden to the client) returns this URL to /index.php as a GET string. The index page instantiates your main framework object. Accessible URLS also help with SEO. URLs are great for below reasons:

  • They look prettier
  • They are easier to remember
  • They help you save filespace on pages which have many links
  • They are easier to link to, both for you and for other webbies
  • They help cut down on typos because there is less confusion about what exactly to write and type

Efficiently Access Database :-
Framework provides the way to access database in an object-oriented way. Instead of directly interacting with the database using SQL queries, you can use any Object-Relational Mapping (ORM) model to manage the structure and relationships between your data. With ORM model, you can map your database table to a PHP class and a row from that table is mapped to an instance of that class.

Conclusion :-
You might not opt for using PHP Framework or using a Framework may or may not be the best choice for you. But, if we are believing with the word โ€œPerformance Really Mattersโ€, then we can try for any popular PHP Framework, and that will be even easier and robust way to develop applications. Frameworks are cool and easy, and we canโ€™t tell what tomorrow is going to bring. I hope this blog will help you to understand what are the advantages of using a Framework and will value Developers.

After reading the above post, are we still thinking, whether to use Framework or not !!! ๐Ÿ˜ ๐Ÿ˜ฎ ๐Ÿ˜ฆ

But, if we got the points and benefits of Framework, then ๐Ÿ˜Ž ;-), we are way to go :). Hope you will like this post and will use Framework, if not till now :mrgreen:.

Thanks ๐Ÿ™‚ and will be happy to listen from you ๐Ÿ™‚ :).