PHP interface, extends,implement,implements function and difference collection and collation

Source: Internet
Author: User
Tags comparable

Extends is inherited from a class, can use the parent class's methods after inheritance, or you can override the parent class's methods, inheriting the parent class, as long as the class is not declared final or the class is defined as abstract to inherit

Implements is to implement a plurality of interfaces, the method of the interface is generally empty, must be rewritten to use, you can effectively, the implementation of the class method name, and parameters are constrained. Can be alternative to the implementation of multiple inheritance.

The method body is implemented in class A extends B implements c,d,e{} class. A interface can extends multiple other interface.

Interface abc{

Const BBQ = 321;

Public Function A1 (show $ACC);

}

Class Test implements abc{

Public Function A1 (show $ACC) {//must function and parameter

return 123;

}

}

$n = new ABC ();

echo $n->a1 ();

Interface Comparable {
function compare (self $compare);
}

Class String implements comparable {
Private $string;
function __construct ($string) {
$this->string = $string;
}

function compare (self $compare) {
if ($this->string = = $compare->string) {
return $this->string. " = = ". $compare->string." <br> ";
}else{
return $this->string. "! = ". $compare->string." <br> ";
}
}
}

Class Integer implements comparable {
Private $integer;
function __construct ($int) {
$this->integer = $int;
}

function compare (self $compare) {
if ($this->integer = = $compare->integer) {
return $this->integer. " = = ". $compare->integer." <br> ";
}else{
return $this->integer. "! = ". $compare->integer." <br> ";
}
}
}

$first _int = new Integer (3);
$second _int = new Integer (4);
$first _string = new String ("foo");
$second _string = new String ("bar");

echo $first _int->compare ($second _int); 3!=4
echo $first _int->compare ($first _int); 3==3
echo $first _string->compare ($second _string); Foo!=bar
echo $first _string->compare ($second _int); Critical Error

Implement is used to extend the use of methods.

Extend the new function
var a=function () {};
var b=function () {};
Function.implement ({
Alert:function (msg) {//Direct alert output content
Alert (msg);
},
Output:function (msg) {//firebug console will output content, IE will error
Console.log (msg);
}
});
A.alert (' 1 ');
A.output (' 2 ');
B.output (' 3 ');

PHP interface, extends,implement,implements function and difference collection and collation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.