Showing posts with label class name. Show all posts
Showing posts with label class name. Show all posts

Wednesday, July 16, 2014

Constant variables in PHP

Basic Concept
By declaring a variable as constant, it will available all over the program and only we can access it, cannot modify it.

In PHP we can declare a constant variable in two ways. That are using define() function and const keywords.

Before going to see deep difference between these, first look a simple program using these two methods.

Example 1:
<?php
define('PIE',3.143);
const SHAPE='Circle';

function fun()
{
       echo PIE.'<br />';
       echo SHAPE;
}

fun();
?>

Output:
3.143
Circle
?>

On above example there no difference between using define() and const. But it has following notable differences.

1. const execute on compile time and define() execute on run time. That is we cannot declare const under a function or and conditional statement like "if".

2. We cannot declare define() in class, but can declare const.

See the below complex example which is using define() and const in all possible manners.

Example 2:
<?php
define('REGISTER_NO',90323540126);  // Declaring a constant using define keyword in global.
const NAME = 'Ramasamy Kasi';          // Declaring a constant using const keyword in global.
const ROLL_NO = 35;

function fun()
{
define('DEPARTMENT','Computer Science'); // Declaring a constant using define inside a funtion.
// const TOTAL = 450;    // Try to declare a constant using const inside a funtion. ITS WRONG.

echo REGISTER_NO.'<br />'; // Accessing consatants inside a function.
echo NAME.'<br />';
}

class Test
{
// define('AVERAGE',90) // Try to declare a constant using define inside a class. ITS WRONG.
const SEMESTER = 1;          // Declaring a constant using const inside a class.

function sample()
{
echo self::SEMESTER.'<br />';          // WAY 1: Accessing class constant.
}
}

fun(); // Calling function.
echo ROLL_NO.'<br />'; // Accessing constant, which is created in side a function.
echo DEPARTMENT.'<br />'; // Accessing constant, which is created on global.
$obj=new Test();
$obj->sample(); // Calling class function.
echo TEST::SEMESTER; // WAY 2: Accessing class constant.
?>

Output:
90323540126
Ramasamy Kasi
35
Computer Science
1
1

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.

Learn JavaScript - String and its methods - 16

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