Wednesday, 27 May 2015

Advance-PHP-Tutorials

Advance   PHP    Tutorials:

Class example:

<?php

class books
{

var $bookname;
var  $price;

function display($name,$cost)
{
echo $this->bookname=$name;
echo '<br>';
echo $this->price=$cost;
echo '<br>';

}
}

$obj=new books();
$obj->display("om",10);

?>
Output:
om
10


Constructor & Destructor in php:


<?php
class  example
{

function  __construct()
{

echo "constructor is called & object  is created";

}

function __destruct()
{

echo "destructor is called & object is free";

}
}
$obj=new  example();

?>
Output:
constructor is called & object is created
destructor is called & object is free

Inheritance in php:

<?php

class  parent1

{

var $name="test";
var $phone="123456";

public function disp()
{

echo $this->name;
echo "<br>";
echo $this->phone;
echo “<br>”;
}
}

class  inheritance1 extends parent1
{

function read()
{

echo "it is working fine";

}


}

$obj=new inheritance1();

$obj->disp();

$obj->read();


?>
Output:
test
123456
it is working fine


interface in php:

<?php

interface  test

{

function disp();

}

class  hello implements test
{


function disp()
{


echo "it is interface function";


}


}

$obj=new  hello();


$obj->disp();


?>

Output:
it is interface function


Static variable & static member function in php:

<?php

class staticexample

{

public static $name="om"; // static variable


public static function disp()  //static member function


{

echo "static member function is this";


}


}


$obj=new  staticexample();


print  staticexample::$name;    // accessing static variable

echo "<br>";
staticexample::disp();        //accessing static member function





?>
Output:
om
static member function is this


Auto load  keywords in php:


First write code for  interface1.php file:
<?php

interface  test

{

function disp();

}

class  interface1  implements test
{


function disp()
{


echo "it is interface function";


}


}

$obj=new  interface1();


$obj->disp();


?>
Output:
it is interface function
And write code for  inheritance1.php file:

<?php

class  parent1

{

var $name="test";
var $phone="123456";

public function disp()
{

echo $this->name;
echo "<br>";
echo $this->phone;

}
}

class  inheritance1 extends parent1
{

function read()
{

echo "it is working fine";

}


}

$obj=new inheritance1();

$obj->disp();

$obj->read();


?>
Output:
test
123456it is working fine
And finally use  following  code autoload these two  files :

<?php

function __autoload($class_name)
{
    include $class_name . '.php';
}

$obj  = new inheritance1();

$obj2 = new interface1();

?>

Output:
(note: It will display boths files output together…)
test
123456it is working fine it is interface function


No comments:

Post a Comment