Object Interfaces

Interface is a class with no data members and contains only member functions and they lack its implementation. Any class that inherits from an interface must implement the missing member function body. In PHP 5 class may inherit only one class, but because interfaces lack an implementation any number of class can be inherited. In PHP 5, interfaces may declare only methods. An interface cannot declare any variables. To extend from an Interface, keyword implements is used. PHP5 supports class extending more than one interface.Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined.

Example:
Code:

 
interface employee
{
	function setdata($empname,$empid);
	function getData();
}
 
class Salary implements employee
{
	function   setdata($empname,$empid)
	{
        $this->empname=$empname;
	   $this->empid=$empid;
	}
 
	function getData()
	{
	    echo "The employee name is: ",$this->empname,"
"; echo "The employee Id is: ",$this->empid,"
"; echo "Inside Salary Class"; } } $a = new Salary(); $a->setData("Cruise",101); $a->getData();

Output:

The employee name is: Cruise
The employee Id is: 101
Inside Salary Class

extends

When you need classes with similar variables and functions to another existing class. The extended or derived class has all variables and functions of the base class,and what you add in the extended definition. It is not possible to subtract from a class. An extended class is always dependent on a single base class, that is, multiple inheritance is not supported. Classes are extended using the keyword ‘extends’.

This is also called a “parent-child” relationship. You can create a class, parent, and use extends to create a new class based on the parent class: the child class. You can even use this new child class and create another class based on this child class.

Example:
Code:

 
class person
{

var $age;
function set_age($age)
{

$this->age=$age;
}
function  get_age()
{
 echo "The age is : " , $this->age ,"
"; } } class student extends person { var $name; function set_name($name) { $this->name=$name; } function get_name() { echo "The name is : " , $this->name; } } $obj=new student; $obj->set_age(23); $obj->get_age(); $obj->set_name("Andy"); $obj->get_name();

Output:

The age is : 23
The name is : Andy

Explanation:

This student that has all variables and functions of person plus an additional variable $name and an additional function set_name() and get_name(). You create a student the usual way and can now set and get the person age. You can still use student functions on student.

class_exists()

This function Checks if the class has been defined.

Syntax: class_exists ( string class_name , bool autoload )

Description:

This function returns TRUE if the class_name has been defined, FALSE otherwise.It will attempt to call __autoload by default, if you don’t want class_exists() to call __autoload, you can set the parameter autoload to FALSE.

Example:
Code:

 
class myclass
{
var $name;
function set_name($name)
{
$this->name=$name;
}

function get_name()
{
echo $this->name;
}

}
if(class_exists('myclass'))
{
$obj=new myclass;
$obj->set_name('sadique');
$obj->get_name();
}
else
{
echo "myclass class is not exist.";
}

Output:

Swan

method_exists()

This function Checks if the class method exists.

Syntax: method_exists ( object, string method_name )

Description:

This function returns TRUE if the method_name has been defined for the given object, FALSE otherwise.

Example:
Code:

  
class calculate
{
function add()
{}

}

$obj=new calculate;
if(method_exists($obj,add))
{
echo "add function is exist";

}
else
{
echo "add function is not exist";
}

Output:

add function is exist

property_exists()

This function Checks if the object or class has a property.

Syntax: property_exists ( mixed class, string property )

Description:

This function checks if the given property exists in the specified class and if it is accessible from the current scope.

The first Parameter “class” is Required.It specifies a string with the class name or an object of the class to test for.

The second parameter “property” is Required.It specifies the name of the property.

It returns TRUE if the property exists, FALSE if it doesn’t exist or NULL in case of an error occur.

Example:
Code:

 

class myClass {
    var $name;
	
}

$obj=new myClass;
if(property_exists('myClass','name'))
{
echo "name property is exist";
}
else
{
echo "name property  is not exist";
}
   

Output:

name property is exist

Page 1 of 212
eXTReMe Tracker