INHERITANCE-INTERFACE-ABSTRACT CLASS-FINAL KEYWORD-PARENT KEYWORD
Inheritance :
PHP class definitions can optionally inherit from a parent class definition by using the extends clause. The syntax is as follows:
class Child extends Parent {
<definition body>
}
The effect of inheritance is that the child class (or subclass or derived class) has the following characteristics:
· Automatically has all the member variable declarations of the parent class.
· Automatically has all the same member functions as the parent, which (by default) will work the same way as those functions do in the parent.
<?php
class myparent
{
var $name="test";
var $phone="123456";
public function disp()
{
echo $this->name;
echo "<br>";
echo $this->phone;
echo "<br>";
}
}
class mychild extends myparent
{
function read()
{
echo "it is working fine";
}
}
$obj=new mychild();
$obj->disp();
$obj->read();
?>
//if you declare a class member as a private then it can be accessed inside class only
<?php
class test
{
private function disp() // can not access outside class
{
echo "display";
}
public function read()
{
$this->disp(); //accessing private member function inside class
echo "</br>";
echo "read function";
}
}
class mychild extends test
{
function ok()
{
echo "it is child function ok";
}
}
$obj=new mychild();
$obj->read();
$obj->ok();
?>
Interfaces :
Interfaces are defined to provide a common function names to the implementors. Different implementors can implement those interfaces according to theri requirements. You can say, interfaces are skeltons which are implemented by developers.
As of PHP5, it is possible to define an interface, like this :
interface Mail {
public function sendMail();
}
Then, if another class implemented that interface, like this :
class Report implements Mail {
// sendMail() Definition goes here
}
Example :
<?php
interface test1
{
function disp();
}
interface test2
{
function read();
}
class hello implements test1,test2
{
function disp()
{
echo "it is interface function of test1";
}
function read()
{
echo "it is interface function test 2";
}
}
$obj=new hello();
$obj->disp();
$obj->read();
?>
=================================================================================
<?php
interface abc
{
function test1();
}
interface abc1 extends abc
{
function hello();
}
class test
{
function test($name)
{
echo $name .'<br>';
}
function test1()
{
echo "test1".'<br>';
}
function hello()
{
echo "hello".'<br>';
}
}
$object=new test("om");
$object->test1();
$object->hello();
?>
output:
om
test1
hello
=================================================================================
<?php
interface abc
{
function test1();
}
interface abc1 extends abc
{
function hello();
}
class test
{
function test($name)
{
echo $name .'<br>';
}
function test1()
{
echo "test1".'<br>';
}
function hello()
{
echo "hello".'<br>';
}
}
$object=new test("om");
$object->test1();
$object->hello();
?>
output:
om
test1
hello
Abstract Classes :
Abstract class vs Interface
1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods.
2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
4) Abstract class can have static methods, main method and constructor. Interface can't have static methods, main method or constructor.
5) Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.
6) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
7) Example:
public abstract class Shape{
public abstract void draw();
} Example:
public interface Drawable{
void draw();
}
an abstract class is one that cannot be instantiated, only inherited. You declare an abstract class with the keyword abstract, like this :
1) Abstract class can have abstract and non-abstract methods. Interface can have only abstract methods.
2) Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
3) Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
4) Abstract class can have static methods, main method and constructor. Interface can't have static methods, main method or constructor.
5) Abstract class can provide the implementation of interface. Interface can't provide the implementation of abstract class.
6) The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
7) Example:
public abstract class Shape{
public abstract void draw();
} Example:
public interface Drawable{
void draw();
}
an abstract class is one that cannot be instantiated, only inherited. You declare an abstract class with the keyword abstract, like this :
When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same visibillity.
abstract class MyAbstractClass {
abstract function myAbstractFunction() {
}
}
Example :
<?php
abstract class Animal
{
public $name;
public $age;
public function Describe()
{
return $this->name . ", " . $this->age . " years old";
}
abstract public function Greet();
}
class Dog extends Animal
{
public function Greet()
{
return "Woof!";
}
}
$animal = new Dog();
$animal->name = "Bob";
$animal->age = 7;
echo $animal->Describe();
echo $animal->Greet();
?>
Static Keyword :
Declaring class members or methods as static makes them accessible without needing an instantiation of the class. A member declared as static can not be accessed with an instantiated class object (though a static method can).
Example :
<?php
Class staticexample
{
public static $name="om"; // static variable
public static function disp() //static member function
{
echo self::name; //can not use $this
echo "static member function is this";
}
}
$obj=new staticexample();
print staticexample::$name; // accessing static variable
echo "<br>";
staticexample::disp(); //accessing static member function
?>
another examples :
class test
{
private static $no_of_call = 0;
private static $name="om";
public function __construct()
{
echo self::$name;
self::$no_of_call = self::$no_of_call + 1;
echo "No of time object of the class created is: ". self::$no_of_call;
}
}
$objT = new test(); // Prints No of time object of the class created is 1
$objT2 = new test();
$obj3=new test(); //Prints No of time object of the class created is 2
?>
So creating
static variable or property is very useful if you want to share some data between
the different object of the same class.
=================================================================================
Use of static keyword:
Suppose you want to implement a PHP script which will tell you how many times you are calling your Function ():
If you use without using static variable then you will see:
Example :
<?php
function call()
{
$c=0;
$c++;
echo $c;
}
echo "first" .call();
echo '<br>';
echo "second" .call();
echo '<br>';
echo "third" .call();
?>
1first
1second
1third
1second
1third
And now example using with static variable ….
<?php
function call()
{
static $c=0;
$c++;
echo $c;
}
echo "first" .call();
echo '<br>';
echo "second" .call();
echo '<br>';
echo "third" .call();
?>
Output:
1first
2second
3third
2second
3third
Final Keyword :
PHP 5 introduces the final keyword, which prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended.
Following example results in Fatal error: Cannot override final method BaseClass::moreTesting()
<?php
class BaseClass {
public function test() {
echo "BaseClass::test() called<br>";
}
final public function moreTesting() {
echo "BaseClass::moreTesting() called<br>";
}
}
class ChildClass extends BaseClass {
public function moreTesting() {
echo "ChildClass::moreTesting() called<br>";
}
}
?>
Parent keyword :
<?php
class A
{
function example()
{
echo "I am A::example() and provide basic functionality.<br />\n";
}
}
class B extends A {
function example() {
echo "I am B::example() and provide additional functionality.<br />\n";
parent::example();
}
}
$b =new B;
// This will call B::example(), which will in turn call A::example().
$b->example();
?>
================================================================================================================================================
Constants :
A constant is somewhat like a variable, in that it holds a value, but is really more like a function because a constant is immutable. Once you declare a constant, it does not change.
Declaring one constant is easy, as is done in this version of MyClass :
Php constant example using define() :
<?php
define("PI","3.142");
$r=2;
echo $area=$r*$r*PI;
?>
Php constant example using “const” :
<?php
class calarea
{
const PI="3.142";
var $r=2;
function area()
{
echo $this->r*$this->r*self::PI;
}
}
$class = new calarea();
$class->area();
?>
================================================================================================================================================
Auto loading Classes using function __autoload() :
(1)WRITE CODE FOR person.php file :
<?php
class person
{
var $name;
function set_name($data)
{
echo $this->name=$data;
}
}
?>
(2)write code for auto.php file :
<?php
function __autoload($class_name)
{
require $class_name.'.php';
}
$tony=new person;
$tony->set_name("Tony");
?>
Comments
Post a Comment