Monday, July 14, 2014

Function overloading in PHP with example

General Definition
Defining more than one function with a same name, but different number of parameters or different types of parameters.

In PHP
PHP does not support function overloading directly. That is we cannot create more than one function with a same name under a class, even it has different number of arguments or different data types. But we can do this using a PHP magic method called __call().

__call()
This PHP magic method will be triggered when class object trying to call undefined function(Note: This will not work when we trying to call a undefined function using class name.).
This has two parameters, first one is function name(Undefined function's name which we are trying to call.) and second one is parameters(Undefined function's parameters which we are trying to input.).

Example
<?php
class A
{
function __call($function,$parameters)  // Magic method
{
//echo $function;
//print_r($parameters);
if($function=='test')
{
switch (count($parameters)) {
case "1":
echo 'Test function with one parameters.<br />';
break;

case "2":
echo 'Test function with two parameters.<br />';
break;
case "3":
echo 'Test function with three parameters.<br />';
break;
default:
echo 'Critical Error.<br />';
break;
}
}
else
{
echo $function.' function not exist.<br />';
}
}
}

$a=new A();
$a->test('hi');
$a->test('par1','par2');
$a->test(1,2,3);
$a->sample();
?>

Output
Test function with one parameters.
Test function with two parameters.
Test function with three parameters.
sample function not exist.

Function overriding in PHP with example

Definition
Rewrite the action of function in sub or derived class, which is already in base or super class.

Example
<?php
class Fruits
{
public function taste()
{
return 'Sweet';
}

public function color()
{
return 'Reddish orange';
}
}

class Greaps extends Fruits
{
public function color()  // Overriding color function.
{
return 'Black';
}
}

$greap=new Greaps();
echo 'Taste: '.$greap->taste().'<br />';
echo 'Color: '.$greap->color();
?>

Output:
Taste: Sweet
Color: Black

Notes:
We can avoid method overriding by declaring a function as final.

Thursday, July 10, 2014

Simple OOPs example program using PHP

Class Page: Books.php
<?php
class Books // Class name.
{
var $title='5'; // Class variables.
var $price=45;

function __construct() // Constructor.
{
$this->price=100; // Define books's default price is 100.
}

function setTitle($title) // Set title for book.
{
$this->title=$title;
}
function getTitle() // Get tiitle of book.
{
return $this->title;
}

function setPrice($price) // Set price for book.
{
$this->price=$price;
}
function getPrice() // Get price of book.
{
return $this->price;
}
}
?>

Main Page: index.php
<?php
include 'Books.php'; // Include class file.

$tamil=new Books(); // Creating object.
$tamil->setTitle('Tamil Text Book'); // Set tittle.
$tamil->setPrice(150); // Set price.

$english=new Books();
$english->setTitle('Spoken English');
$english->setPrice(180);

$environment=new Books();
$environment->setTitle('Environmental Science');

echo $tamil->getTitle() . ' : ' . $tamil->getPrice().'<br />'; // Display save title and price.
echo $english->getTitle() . ' : ' . $english->getPrice().'<br />';
echo $environment->getTitle() . ' : ' . $environment->getPrice().'<br />';
?>

Output:
Tamil Text Book : 150
Spoken English : 180
Environmental Science : 100

Tuesday, July 1, 2014

autocomplete - Remove user entered or update data on page refresh.

Situation
On real world we often repopulate user's previously saved data and ask them to update these details.
For example a Profile Page. On here some time user may try to reload the page after altered some data
on form, to get his/her original previous data(which means the data came from server). But browser does not act as he/she thinking. Browser will catch the form data and repopulate what he enter last time, not original data.

Solution
To avoid this we can use a html attribute called autocomplete. Simply set this attribute to false on your form tag.

Example 1:

<form name="profile" id="profile" method="post" action="profile_action" autocomplete="off">

We can use for a particular inputs only, as like below.


Example 2:

<input type="text" name="email" id="email" autocomplete="off" />

Note: By default autocomplete set to on for all.

Wednesday, June 4, 2014

10 Interview Mistakes To Avoid

My first Android App - Hello World

I am very happy to announce to the world, I made 'Hello World' app on Android.

This is my first, 'Hello World' app on android. Here I just used two activity, one for get user input and another one for display that input. That's all...

Here I attached my project's source code and APK file. For install on your android device just download and open the APK file.





Tuesday, February 18, 2014

Set Wired Network Connection On Ubuntu

1. Click on network option on top right.

2. Then navigate to “VPN Connections” and click “Configure VPN”.

3. Click “Add” on “Wired” tap.

4. Now enter a name to your new connection.

5. And select “IPv4 Settings” option and select “Manual” on “Method” option.

6. Now click “Add”, and enter following values on specific fields.

7. Address: <Your system IP> Example: 192.164.1.7
                   Note: You can get it via entering “ipconfig” in ubuntu terminal.
  Netmask: 255.255.255.0
  Gateway: <Your router IP / Gateway> Example: 192.164.1.1
                   Note: You can get it via entering “route -n” in ubuntu terminal.
  DNS servers: 8.8.8.8
  Search domains: 8.8.4.4

8. Check “Available to all users” check box, if you wish. So that anyone on that system can use this config.

9. Now click “Apply”.

Learn JavaScript - String and its methods - 16

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