- BASICS
- Quotes
- Constants
- Control Structures
- Reference
- Number Systems
- VARIABLES
- Definition
- Variable Variable
- Exists
- Type Casting
- OPERATORS
- Aritmetic
- Bitwise
- String
- Comparison
- Logical
- FUNCTION
- Definition
- Anonymous
- Reference
- Variable Arguments
- ARRAY
- Basics
- Operations
- Create
- Search
- Modify
- Sort
- Storage
- STRING
- String Basics
- String Compare
- String Search
- String Replace
- String Format
- String Regexp
- String Parse
- Formating
- Json
- STREAMS
- File Open
- Read File
- Read Csv
- File Contents
- Context
- Ob_start
- OOP
- Object Instantiation
- Class Constructor
-
Interfaces, Abstract
- Resource Visibility
- Class Constants
- Namespaces
- HTTP
- Headers
- File Uploads
- Cookies
- Sessions
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: 531 days ago