The interface uses interfaces, you can specify that a class must implement those methods, but you do not need to define the specifics of these methods, we can define an interface through interface, just as you would define a standard class, but all of the methods in the definition are empty. All methods defined in the interface must be public to implement an interface, you can use the implement operator, the class must implement the method defined in the interface, if you implement multiple interfaces, you can use commas to split the names of multiple interfaces when implementing multiple interfaces, The methods in the interface can not have the same name interface or inheritance, by using the extends operator interface can be defined constants, interface constants and class constants are used exactly the same, they are fixed value, can not quilt class or sub-interface Modification Example://Declaration Interface Interface itemplate{ Public Function SetVariable ($name, $var); Public Function gethtml ($template);} Implement Interface class Template implements itemplate{private $vars =array (); Public function setvariable ($name, $var) {$this vars[$name]= $var; The Public Function gethtml ($template) {foreach ($this->vars as $names = + $value) {$template =str_replace (' {'. $names. '} ' , $value, $template); } return $template; }} example 2:interface a{public Function foo ();} Interface b{public Function bar (); Inherit interface interface C extends a,b{public function Baz ();} Implement Interface Class D implements c{public function foo () {} public Function bar () {} public Function Baz () {}} interface a{const b= "Hello";} Abstract classes PHP5 support abstract classes and abstract methods. An abstract class cannot be instantiated directly, it must first inherit the abstract class, and then instantiate the subclass, any class, if he is inside theFew of the methods are declared abstract, and the class must be declared abstract, and if the class method is declared abstract, then the subclass must implement all the abstract methods in the abstract class if it does not include the specific function implementation to inherit an abstract class, in addition, The visibility of these methods must be the same as (or more easily) in the abstract class, and if an abstract method in the abstract class is declared as proteected, then the methods implemented in the subclass should be declared as pritected or public: abstract class abstractclass{//Abstract method Abstraction protected function getValue (); abstract protected function prefievalue ($p);//Common method (non-abstract method) Public Function PrintOut () {print $this->getvalue ();}} Class ConcreteClass1 extends abstractclass{protected function GetValue () {return ' ConcreteClass1 ';} public Function pref Ievalue ($p) {return ' {$p}concreteclass1 ';}} Class ConcreteClass2 extends abstractclass{protected function GetValue () {return ' ConcreteClass2 ';} public Function pref Ievalue ($p) {return ' {$p}concreteclass2 ';}} $class 1=new ConcreteClass1 (); $class 1->printout (); Echo $class 1->prefievalue (' foo_ ');/* Print Result: concreteclass1foo_concreteclass12. It is still possible to call the abstract class of a static method to a class constant that does not have the abstract class foo{const a= "Hello"; static function bar () {echo "test\n";}} foo::a;//no output Foo::bar ();//test "
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