Showing posts with label class. Show all posts
Showing posts with label class. 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

Class inheritance in PHP

Definition
Creating a new class by inheriting(combining) exist class is called inheritance. By doing this newly created class can access existing classes functions and variables. Here newly created class will be called as sub class or derived class and the class from where it inherited is called as base class or super class.

Example
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;
}

public function name() // A public function.
{
return '<br />Ramasamy Kasi : Public funtion from Books(base) class.';
}

protected function mobileNumber() // A protected function.
{
return '<br />+91 95684575000 : Protected funtion from Books(base) class.';
}

private function password() // A private function.
{
return '<br />bossword : Private funtion from Books(base) class.';
}

protected function getPassword() // Call private function from the same class(Books).
{
return $this->password();
}

function __destruct() // Destructor.
{
echo '<br />Destructor for "'.$this->title.'" Called.'; // Will be called when, there is no further action exist for a object.
}
}
?>

EBooks.php:
<?php
class EBooks extends Books         // Creating new class by combining existing class.
{
var $link;

function setLink($link) // Set link for book.
{
$this->link=$link;
}

function getLink() // Get link for book.
{
return $this->link;
}

function mobileNumber() // Getting mobile number by directly calling base classes protected function 'mobileNumber'.
{
$mob_from_base=Books::mobileNumber();
return $mob_from_base.' - Through EBooks(Sub) Class.';
}

function password() // Getting password by calling one base classes public function 'getPassword'.
{
$pass_from_base=$this->getPassword();
return $pass_from_base.' - Through EBooks(Sub) Class.';
}
}
?>

index.php:
<?php
include 'Books.php'; // Include Books class file.
include 'EBooks.php'; // Include EBooks 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');

$python=new EBooks();
$python->setTitle('Python for Beginners');
$python->setLink('http://python.books/python_begin.html'); // Set link.
$python->setPrice(85);

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

echo Books::name(); // Calling a public function of base class(Books).
echo EBooks::mobileNumber(); // Calling a protected function of base class(Books) by calling public function of sub class(EBooks).
echo $python->password().'<br />'; // Calling a private function of base class(Books) by calling public function of sub class(EBooks).
?>

Output
Tamil Text Book : 150
Spoken English : 180
Environmental Science : 100
Python for Beginners : 85 : http://python.books/python_begin.html

Ramasamy Kasi : Public funtion from Books(base) class.
+91 95684575000 : Protected funtion from Books(base) class. - Through EBooks(Sub) Class.
bossword : Private funtion from Books(base) class. - Through EBooks(Sub) Class.

Destructor for "Python for Beginners" Called.
Destructor for "Environmental Science" Called.
Destructor for "Spoken English" Called.
Destructor for "Tamil Text Book" Called.

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

Wednesday, July 24, 2013

Apply Even and Odd classes for elements on, "while" or "for" loop

When we display list of elements using any looping, we need to add some two classes for differentiate one row from another. On this case we need use one extra variable, and increment it throw loop, then we can get '0' or '1' by mod(%) this value. So now we can have two different classes repeatedly like 'r0','r1'. And now write style for these two classes.

Example:
<style type="text/css">
Output
.list0{
background-color: #00B2C1;
}
.list1{
background-color: #50D4FF;
}
</style>
<html>
<title>Even and Odd classes for loopig elements</title>
<body>
<table cellspacing="0">
<tr>
<td>NO</td>
<td>Name</td>
</tr>
<?php $i=0; while($i<=10) { ++$i; ?>
<tr class="list<?php echo (++$x%2); ?>">
<td><?php echo $i; ?></td>
<td>Name <?php echo $i; ?></td>
</tr>
<?php } ?>
</body>
</html>


Note: Here we can use '$i' to mod. But most of cases we don't has this option, and more over here we wrote single line(++$x%2) to get mod value.

Learn JavaScript - String and its methods - 16

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