interface--Interface class

Source: Internet
Author: User

<?php/** * In order to declare an interface, you need to use the keyword interface *interface iexampleinterface {} * Description (most developers choose to prefix with the capital letter I in front of the section name. In order to differentiate it from the class in the code and the generated document) * and to inherit the abstract class using the extends keyword, the implementation interface uses the Implements keyword. *class ExampleClass implements Iexampleinterface {} * mentioned earlier, the point that interfaces are better than abstract classes is that each class can use multiple interfaces. When you want to implement two or more than two interfaces in a class, we can separate them with commas. For example, if you have an object with an array style and want it to have both iteration and count capabilities, then we can define a class like this. *class Myarraylikeobject implements Iterator, countable {} * * * * interface Ispeedinfo {function Getmaximumspeed (); } class Car {Any base class method} class Fastcar extends Car implements Ispeedinfo {function Getmaximumspeed () {return 150;}} class Badcar extends fastcar{} $a = new Fastcar (); echo $a->getmaximumspeed ();echo ' <br/> '; Class Street extends Fastcar {protected $speedLimit;protected $cars;Public function __construct ($speedLimit = 200) {$this->cars = Array (); Initialize variables$this->speedlimit = $speedLimit;} function Isstreetlegal ($car) {if ($car instanceof Ispeedinfo) {if ($car->getmaximumspeed () < $this->speedlimit) {return true;} else {return false;}} else {Extension classes must implement Ispeedinfo to make street legalreturn false;}}Public Function Addcar ($car) {if ($this->isstreelegal ($car)) {Echo ' The Car was allowed on the road.$this->cars[] = $car;} else {Echo ' The Car is too fast and being not allowed on the road.}}}/* Class Street extends fastcar{} */$a = new Street (); echo $a->getmaximumspeed (); 150

interface--Interface class

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.