Skip to content

How to declare dependencies and use composer in php

Using Composer:

We will now use Composer to install the dependencies of the project. To use Composer in your project, you’ll need one file: composer.json. In this file you will describe the dependencies of your project. So let’s create the file:

vim composer.json

Declaring Dependencies:

Once you created a composer.json file within your project which describes the project’s dependencies.

    "require": {
        "package-name 1": "version",
        "package-name 2": "version"

Installing Dependencies:

Once you updated your composer.json file with required packages, now you are able to install the mentioned packages.

To resolve and download dependencies, run the install command:

$ php composer.phar install

If you did a global install and do not have the phar in that directory run this instead:

$ composer install

Upgrade the dependent libraries to newer versions, you can run the following command:

$ php composer.phar update
$ composer update

Autoloading – Include packages into your project:

Another cool thing about Composer is its autoloading feature. For those libraries that provide autoloading information, Composer automatically generates an autoload.php file directly in the vendor folder that you can include in your project. Then you can directly start using the classes from those libraries. In your PHP project, you can just specify this:

require 'vendor/autoload.php';

For more information: visit


How To Install and Use Composer on Ubuntu


Composer is a tool for dependency management in PHP. It allows you to declare the dependent libraries your project needs and it will install them in your project for you.

Dependency management

Composer is not a package manager. Yes, it deals with “packages” or libraries, but it manages them on a per-project basis, installing them in a directory (e.g. vendor) inside your project. By default it will never install anything globally. Thus, it is a dependency manager.

This idea is not new and Composer is strongly inspired by node’s npm and ruby’s bundler. But there has not been such a tool for PHP.

The problem that Composer solves is this:

a) You have a project that depends on a number of libraries.

b) Some of those libraries depend on other libraries.

c) You declare the things you depend on.

d) Composer finds out which versions of which packages need to be installed, and installs them (meaning it downloads them into your project).


1) PHP 5.3.2+

2) Curl


$ curl -s | php
$ sudo mv composer.phar /usr/local/bin/composer.phar

If you want, you can create an alias:

$ alias composer='/usr/local/bin/composer.phar'

This way you can invoke composer with just composer, else you need to invoke by composer.phar

How to download file from remote URL in PHP

There are many ways in PHP to download file from the remote server. We can use php functions like copy, file_get_content, fopen, fsockopen & Curl to download the remote files. For your reference here I have explained about copy, fopen and Curl method, I have choosen these methods based on simplify and easily everyone can understand & use it. We can download large file with minimum memory usages.

Method 1:
// Copy method
$fullPath = "http://serverurl";
$path_parts = pathinfo($fullPath);
$my_file = urldecode($path_parts['basename']);
 echo "Download successful";
 echo "Unable to download file";
Method 2:
// Curl method
$url ="http://serverurl";
$file = basename($url);
// Open file to write
$fp = fopen($file, 'w+');
$ch = curl_init($url);
// increase timeout to download big file
curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT, 10 );
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
echo (filesize($file) > 0)? "Download successful": "Unable to download file";

Method 3:
// File method
$fullPath = "http://serverurl";
$path_parts = pathinfo($fullPath);
$handle = @fopen($fullPath, "r");
$data = "";
while(!feof($handle)) {
 $data .= fread($handle, 2048);
fclose ($handle);
$my_file = urldecode($path_parts['basename']);
$handle = fopen($my_file, 'w') or die('Cannot open file: '.$my_file);
fwrite($handle, $data);
fclose ($handle);

Use Number() instead of parseInt() in javascript

Recently I came across the issue in converting string to integer in Javascript(JS). I was used parseInt() method to convert string value into integer value. unfortunately, I was faced issue in chrome browser but all other browsers like firefox, ie and safari are worked fine.

Its too hard time for me to find out the mistake in code, After a long time of struggling with code I came to know the problem with parseInt() method.

In chrome browser

parseInt('08'); // return 0

parseInt('09'); // return 0

Because the function tries to determine the correct base for the numerical system used. In Javascript numbers starting with zero are considered octal and there’s no 08 or 09 in octal.


Instead of parseInt() method use Number() method, It will solve our problem. It will exactly convert the string value into numeric value.

Accessing external website from PhoneGap application

To create a PhoneGap application, To load external webpage into android application by following the instruction.


1) Create phonegap application and configure with android.

2) Then open your activity file and make the changes, onCreate() method is calling super.loadUrl(“”); so that page is opened in the WebView as soon as application starts

super.loadUrl(“file:///android_asset/www/index.html”); change to super.loadUrl(“”);

3) Now it will load external website in android application.

Sample code for your reference:

import org.apache.cordova.CordovaActivity;
import android.os.Bundle;

public class RssFeed extends CordovaActivity
public void onCreate(Bundle savedInstanceState)
super.setIntegerProperty("loadUrlTimeoutValue", 70000); // The connection to the server was unsuccessful. URL=, 7000 Milliseconds which equals 7 seconds.
super.setStringProperty("loadingDialog", "Loading…"); // Loading screen until the web page load

Serailization & Unserialization in PHP

Serialization which help us to store or transport PHP data structure such as an array or object. All the storage meduim or database can store String type. It will generates a storable representation of a value. This is useful for storing or passing PHP values around without losing their type and structure.

Essentially, it takes a php array or object and converts it to a string (which you can then transmit or store as you see fit).

i.e, serialize() allows you to store one of these structs as a string. It can be de-serialised from its string representation with unserialize().

If you are familiar with json_encode() and json_decode()  both the concept is similar.

Using Serialize method, We can store multiple field key & its values in single column. You can easily extend your forms without changing table structure.

Dynamically create form in javascript on fly

To create a form dynamically using JavaScript on fly of the webpage. This which helps to create form and input controls dynamically.


/* To create form with javascript */

var form = document.createElement("form");
form.setAttribute(‘method’,"post"); // form method
form.setAttribute(‘action’,"test.php"); // form action url
form.setAttribute(‘name’,"frmName"); // form name
form.setAttribute(‘id’,"frmId"); // form ID
form.setAttribute(‘target’,"_blank"); // form target location

/* To create input control with javascript */
var inputCtrl = document.createElement("input");
inputCtrl.type = "hidden"; // input control type = "data"; // input control name
inputCtrl.value = "test"; // input control value

/* To bind created input control with created from */

/* To bind created form with current document body */

/* Now you can submit created form */