minte9
LearnRemember



Abstract

Abstract and Interfaces are design to create a series of constrains. Here the class fails to proper implement interface methods.
     
abstract class myAdapter
{
    private $id;
    abstract function insert();
    abstract function update();
    public function save()
    {
        // ...
    }
}

class PDO_myAdapter extends myAdapter{

    public function __construct($dsn)
    {
        // ...
    }

    public function insertAdapter()
    {
        // wrong method name 
        // it should be insert()
    }

    public function update()
    {
        // ...
    }

    // Fatal error: Class PDO_myAdapter contains 1 abstract method ...
    // myAdapter::insert()
}

Interface

Interfaces specifies an API that a class must implement. Interfaces contain no body. Here the class method save() is not compatible with the interface.
 
interface myAdapter
{
    public function insert();
    public function update();
    public function save($name=null);
}

class PDO_Adapter implements myAdapter
{
    public function insert()
    {
        // ...
    }

    public function update()
    {
        // ...
    }

    public function save($name)
    {
        // wrong
    }

    // Fatal error
    // must be compatible with myAdapter::save($name = NULL)
}

Extend

A class can only extend only one parent. It can implement multiple interfaces.
 
class PDO_Adapter extends PDO 
     implements myAdapter, SeekableIterator
{
    // ...
}

Check instance

You can check if an object is an instance of a particular class.
     
class A 
{
    // ...
}

$obj = new A();

if ($obj instanceof A) { // true

    echo "Object is an instance of A";
}



  Last update: 234 days ago