Php abstract classes and interfaces

Source: Internet
Author: User
Php abstract classes and interfaces "interfaces use interfaces. you can specify the methods that a class must implement but do not need to define the specific content of these methods. we can define an interface through interfaces, just like defining a standard class, all the methods defined in the interface are empty. all the methods defined in the interface must be public to implement an interface. you can use the implement operator, class must implement the methods defined in the interface. if multiple interfaces are implemented, you can use commas to separate the names of multiple interfaces. Note that when multiple interfaces are implemented, methods in the interface cannot have or inherit from the interface with duplicate names. constants can be defined by using the extends operator interface. the use of interface constants and class constants is identical. they are all values, examples of modifying quilt classes or subinterfaces are as follows: // Declaring interface Itemplate {public function setVariable ($ name, $ var); public function getHtml ($ template );} // implement the class Template interface Implements Itemplate {private $ vars = array (); public function setVariable ($ name, $ var) {$ this-> vars [$ name] = $ var ;} 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 () ;}// inherits interface c extends a, B {public function baz ();}/ /Implement the class d implements c {public function foo () {} public function bar () {} public function baz () {}} interface a {const B = "hello";} abstract class php5 supports abstract classes and abstract methods. An abstract class cannot be directly instantiated. it must first inherit the abstract class and then instantiate the Subclass. if at least one of its methods is declared as abstract, this class must be declared as abstract. if the class method is declared as abstract, it cannot include specific functions to inherit an abstract class, subclass must implement all abstract methods in the abstract class. In addition, the visibility of these methods must be the same as (or easier) in the abstract class. If an abstract method in the abstract class is declared as proteected, then the method implemented in the subclass should be declared as pritected or public example: abstract class AbstractClass {// abstract method abstract protected function getValue (); abstract protected function prefieValue ($ p ); // normal method (non-abstract method) public function printOut () {print $ this-> getValue () ;}} class ConcreteClass1 extends AbstractClass {protected function getValue () {return "ConcreteClass1";} public function prefieValue ($ p) {return "{$ p} concreteclass1" ;}} class ConcreteClass2 extends AbstractClass {protected function getValue () {return "ConcreteClass2";} public function prefieValue ($ p) {return "{$ p} concreteclass2" ;}}$ class1 = new ConcreteClass1 (); $ class1-> printOut (); echo $ class1-> prefieValue ('foo _ ');/* print the result: ConcreteClass1FOO_concreteclass12. abstract class Foo {const a = "hello"; static function bar () {echo "test \ n ";}} foo: a; // no output Foo: bar (); // test"

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.