Showing posts with label simple example. Show all posts
Showing posts with label simple example. Show all posts

Tuesday, July 15, 2014

Global variables in PHP

Basic Concept
By declaring a variable as Global(Common), we can access it any where of the program.

In C or C++
Here they don't use any special keyword to denote the variable as global. Say for example you defined  or declared a variable with a name in top of the program, now if you use the same name in any where of your program even inside a function, it will denote that as global variable.

Example 1:
#include<stdio.h>
// Global variables
int A=5;
int B=7;

int Add()
{
return A + B;
}

void main()
{
int answer; // Local variable
answer = Add();
printf("%d\n",answer);   // Output: 12
}

In PHP
Here to use global variable inside a function, first we have to declare it as "global".

Example 2:
<?php
$a = 1;
$b = 2;

function Sum()
{
    global $a, $b;
    $b = $a + $b;
}

Sum();
echo $b;   // Output: 3
?>

And one more way is we can use a super global valiable called $GLOBALS, intead of the keyword global.

Example 3:
<?php
$a = 1;
$b = 2;

function Sum()
{
    $GLOBALS['b'] = $GLOBALS['a'] + $GLOBALS['b'];
}

Sum();
echo $b;   // Output: 3
?>

We could not directly declare a class property as global variable. But we can get global variable under a function inside a class. So it can done by assigning global variable value on a property inside the constructor function.

Example 4:
<?php
$a=5;
class A
{
// private global $a; This is wrong statement. It will not going work.
        privare $a;

function __construct()
{
global $a;
$this->a=$a;
}

 function fun1()
{
global $a;
echo 'value of "a" inside class A is: '.$a;
}
}

$obj=new A();
$obj->fun1();
?>

References:
http://php.net/manual/en/language.variables.scope.php
http://www.codingunit.com/c-tutorial-functions-and-global-local-variables

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

Learn JavaScript - String and its methods - 16

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