Showing posts with label object. Show all posts
Showing posts with label object. Show all posts

Friday, July 18, 2014

Abstract class in PHP

Concept
Abstract class cannot be instantiated, that is we can not create a object. Any class which has at-least one abstract function should be  declared as abstract. These abstract functions are only can declare, can not define here. But these are must be defined in classes which are inherited this abstract class. We can declare this abstract function as any type(public, protected or private) in abstract class, but it should be same or less restricted type in inherited class(For example declared as protected may had public in derived class). And number of arguments also should be same(May had additional optional argument in derived class.).

Clear Example
<?php
abstract class A                          // Abstract class.
{
abstract protected function fun1();   // Declaring abstract function as protected.
abstract public function fun2($i);  // Declaring abstract function as public with one argument.
abstract public function fun3($i,$j); // Declaring abstract function as public with two arguments.

public function fun4()  // Declaring and defining a normal function in abstract class.
{
echo 'fun4 definition from class A(abstract class).<br />';
}
}

class B extends A
{
public function fun1()  // Definiion for a abstract function.
{
echo 'fun1 definition from class B.<br />';
}

public function fun2($i)  // Definiion for a abstract function with exact no.of arugument.
{
echo 'fun2 definition from class B.<br />';
}

public function fun3($i,$j,$k=30)  // Definiion for a abstract function with optional arugument.
{
echo 'fun3 definition from class B.<br />';
}
}

$b=new B();
echo $b->fun1();
echo $b->fun2(10);
echo $b->fun3(10,20);
echo $b->fun4();
?>

Abstract vs Interface
1. Abstract is a class but interface not. So memory allocation for abstract is high.
2. Abstract class can had normal functions and variables. But interface cannot.
3. Functions on interface must be public but in abstract class may had anything.
4. A class can inherit only one abstract class but can inherit more than one interface.

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.

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.

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, January 22, 2014

"data" attribute in HTML5 and JQuery

This is used for store a value(its may string, array or object) on a html element.


Setting "data":
<input type="text" id="city" data-info="Main City" />      // Setting via html.
$('#city').data('city', "Main City");                                      // Setting via JS.
$('#city').data('city', ["blue","green","red"]);                      // Store array. 
$('#city').data('city', {special:"River",issue:"Factory"});    // Store object.

Getting Data:
var detail=$('#city').data();

Note: Do not use capital letters on "data" attribute name. Its not working.

Thursday, August 22, 2013

Javascript Local Storage

Reference: http://www.htmldog.com/guides/javascript/advanced/localstorage/

When building more complex JavaScript applications, it’s very useful to be able to store information in the browser, so that the information can be shared across different pages.

Unlike cookies, local storage can only be read client-side - that is, by the browser and your JavaScript. If you want to share some data with a server, cookies may be a better option.

Set Item: localStorage.setItem('name', 'tom');

Get Item: var name = localStorage.getItem('name');


Example:

<html>
      <head> 
              <title>Javascript Local Storage</title>
      </head>
      <body>
              Give some input to store on local: <input type="text" id="input_msg" />&nbsp&nbsp              <input type="button" value="Submit" id="submit" onclick="saveOnLocal();" />
              <br /><br />
              Data from local: <span id="local_data"></span>
      </body>
</html>

<script type="text/javascript">
function saveOnLocal()
{
var data=document.getElementById('input_msg').value;
localStorage.setItem('input_msg',data); //Set local storage
document.getElementById('local_data').innerHTML=localStorage.getItem('input_msg'); //Get local storage
 localStorage.removeItem('input_msg'); //Clear specific local storage item
 //localStorage.clear(); //Clear all local storage item

}
</script>

You can only save string using javascript local storage, use JSON for save array.

Example:
<script type="text/javascript">
localStorage.setItem('name', JSON.stringify(your_array));
var user = JSON.parse(localStorage.getItem('name'));
</script>


Browser support for local storage is not 100% but it’s pretty good - you can expect it to work in all modern browsers, IE 8 and above, and in most mobile browsers too. And the storage size depends on the browser.




Learn JavaScript - String and its methods - 16

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