OBJECT interface, implements AND instanceof IN PHP

Object Interfaces

Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled.

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.

All methods declared in an interface must be public, this is the nature of an interface.

implements
To implement an interface, the implements operator is used. All methods in the interface must be implemented within a class; failure to do so will result in a fatal error. Classes may implement more than one interface if desired by separating each interface with a comma.

Note: A class cannot implement two interfaces that share function names, since it would cause ambiguity.

interface a
{
    public function foo();
}

interface b
{
    public function bar();
}

interface c extends a, b
{
    public function baz();
}

//LET US IMPLEMENTS NOW THE INTERFACES

class d implements c
{
    public function foo()
    {
	echo 'I am class d iplements c foo() interface a <br />';
    }

    public function bar()
    {
	echo 'I am class d iplements b bar() interface b <br />';
    }

    public function baz()
    {
	echo 'I am d iplements c baz() interface c extends a and b <br />';
    }
}

d::foo();
d::bar();
d::baz();

/*
OUTPUT:
I am class d iplements c foo() interface a
I am class d iplements b bar() interface b
I am d iplements c baz() interface c extends a and b
*/

$a = new d;
$a->foo();
$a->bar();
$a->baz();

/*
OUTPUT:
I am class d iplements c foo() interface a
I am class d iplements b bar() interface b
I am d iplements c baz() interface c extends a and b
*/
$myvar_foo = 'foo';
$myvar_bar = 'bar';
$myvar_baz = 'baz';

$a->$myvar_foo();
$a->$myvar_bar();
$a->$myvar_baz();

/*
OUTPUT:
I am class d iplements c foo() interface a
I am class d iplements b bar() interface b
I am d iplements c baz() interface c extends a and b
*/

//LET US IMPLEMENTS a, b TO Class e

class e implements a, b
{

    public function foo()
    {
	echo 'I am class e iplements a foo() interface a <br />';
    }

    public function bar()
    {
	echo 'I am class e iplements b bar() interface b <br />';
    }
}

e::foo();
e::bar();

/*
OUTPUT
I am class e iplements a foo() interface a
I am class e iplements b bar() interface b 

you can use different instantiation above also.
*/

//EXAMPLE OF ACTUAL IMPLEMENTATION.

interface My_Data_Controller
{
	public function search();
	public function sorting();
	public function get_data();
	public function delete();
	public function add();
	public function edit();
	public function mailto();

}

//so in your CUSTOMERS, EMPLOYEES, SUPPLIER etc... you are forcing Classes to use your public interfaces.
class Customers implements My_Data_Controller {}
class Employees implements My_Data_Controller {}
class Suppliers implements My_Data_Controller {}

instanceof
PHP has a single type operator: instanceof is used to determine whether a given object, his parents or their implemented interfaces are of a specified object class.

The instanceof operator was introduced in PHP 5. Before this time is_a() was used but is_a() has since been deprecated in favor of instanceof.


//see also instanceof

class A { }
class B { }

$thing = new A;

if ($thing instanceof A) {
    echo 'A';
}
if ($thing instanceof B) {
    echo 'B';
}

/*
OUTPUT
A
*/

ANOTHER EXAMPLE OF TOY FACTORY IMPLEMENTS AND INSTANCE


/* Factory and Toy interfaces */
 
interface ToyFactory {
    public function makeToy();
}
 
 
interface Toy {
    public function getType();
}
 
/* Concrete implementations of the factory and Toy */
 
class SedanFactory implements ToyFactory {
    public function makeToy() {
        return new Transformer(); //see the instantiation of the class here!
    }
}
 
class Transformer implements Toy {
    public function getType() {
        return 'Bumble Bee!';
    }
}
 
/* Client */
 
$factory = new SedanFactory();
$Toy = $factory->makeToy();
print $Toy->getType();

/* OUTPUT
Bumble Bee!
*/
Advertisements
This entry was posted in PHP and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s