Interface
The interface is to define the common behavior of different classes and then implement different functions within different classes. When a lot of people develop a project together, may go to call someone else to write some of the class, then you will ask, how do I know how he is the implementation of a function of how to name it, this time the PHP interface class interface play a role, when we define an interface class, The way inside it is that the following subclasses must be implemented, such as:
Polymorphic
Because the interface can be implemented in a number of ways, so for the interface inside the top of the implementation of the method is a variety of, this feature is called polymorphism. Polymorphism refers to the ability to redefine or change the nature and behavior of a class in an object-oriented context based on the contexts in which it is used.
PHP does not support overloading to implement polymorphism, but PHP can be variable to achieve polymorphic effects.
Case one:
name;
}
Public Function SetName ($_name) {
$this->name = $_name;
}
}
Class useradmin{//operation.
public static function Changeusername (User $_user,$_username) {
$_user->setname ($_username);
}
}
$normalUser = new Normaluser ();
Useradmin::changeusername ($normalUser, "Tom");//Here is an instance of Normaluser.
echo $normalUser->getname ();
? >
Case TWO:
Abstract class
An abstract class is between an interface and a class definition
PHP5 supports abstract classes and abstract methods. Abstract classes cannot be instantiated directly, you must first inherit the abstract class, and then instantiate the subclass. The abstract class must contain at least one abstract method. If a class method is declared abstract, it cannot include a specific function implementation.
When inheriting an abstract class, the subclass must implement all the abstract methods in the abstract class, and the visibility of these methods must be the same as (or more loosely) in the abstract class. If an abstract method in an abstract class is declared as protected, the method implemented in the subclass should be declared as protected or public, not private.
GetValue (). " ";
}
}
Class ConcreteClass1 extends AbstractClass
{
protected function GetValue () {return
"ConcreteClass1";
} Public
function Prefixvalue ($prefix) {return
"{$prefix}concreteclass1";
}
}
Class ConcreteClass2 extends AbstractClass
{public
function GetValue () {return
"ConcreteClass2";
} Public
function Prefixvalue ($prefix) {return
"{$prefix}concreteclass2";
}
}
$class 1 = new ConcreteClass1;
$class 1->printout ();
echo $class 1->prefixvalue (' Foo_ '). " ";
$class 2 = new ConcreteClass2;
$class 2->printout ();
echo $class 2->prefixvalue (' Foo_ '). " ";
? >