PHP Interface implements use

Source: Internet
Author: User
Tags comparable vars

Mainly for the class name, the methods owned by the class, as well as the parameters of the arguments to do with constraints and specifications to do, it feels like PHP abstract class and a bit similar.

One, the definition and invocation of the interface

  1. <?php
  2. Interface Face1
  3. {
  4. const PARAM = ' test ';
  5. public function Show ();
  6. }
  7. Class Test implements Face1
  8. {
  9. public function Show ()
  10. {
  11. echo "interface is run<br>";
  12. }
  13. }
  14. $face = new Test ();
  15. echo $face->show (); //inerface is run
  16. echo Face1::p Aram; //test
  17. ?>

Note: The above example should be noted that the method name of the interface is show, inherit the interface class must have show this method, otherwise will be error. That is, the method of the interface is false, the real function is in the inheriting class method, because of this, so I think, interface root PHP abstract class A bit like.

Second, the parameter constraint is relatively strict

  1. <?php
  2. Interface Face1
  3. {
  4. public function Show (show $show);
  5. }
  6. Show Normal
  7. Class Test implements Face1
  8. {
  9. public function Show (show $show)
  10. {
  11. echo "ASDFASDF";
  12. }
  13. }
  14. Report fatal error
  15. Class Test2 implements Face1
  16. {
  17. public function Show (AAA $aaa)
  18. {
  19. }
  20. }
  21. ?>

Explanation: The above example reported fatal error, why will the report fatal error? The reason is that the argument is the AAA $aaa, not the show $show. In the Inherited interface class, when invoking the method of an interface, the parameters to be passed and the parameter names in the interface must be one to. Otherwise you will get an error.

Third, inheritance between interfaces and calling interface pass parameters

  1. <?php
  2. Interface Face1
  3. {
  4. public function Show ();
  5. }
  6. Interface Face2 extends Face1
  7. {
  8. Public function Show1 (test1 $test,$num);
  9. }
  10. Class Test implements Face2
  11. {
  12. public function Show ()
  13. {
  14. echo "ok<br>";
  15. }
  16. Public function Show1 (test1 $test,$num)
  17. {
  18. Var_dump ($test);
  19. echo $test 1->aaaa."  $num <br> ";
  20. }
  21. }
  22. Class Test1
  23. {
  24. public $aaaa ="This is a test";
  25. function Fun () {
  26. echo ' ===============<br> ';
  27. }
  28. }
  29. $show = new Test1;
  30. $show->fun (); //Display ===============
  31. Test::show (); //show OK
  32. Test::show1 ($show, 6); //object (test1) #1 (1) {["AAAA"]=> string () "This is a test"} 6
  33. ?>

Note: The above example can be seen, interface Face2 inherits the interface Face1, class test inherits the interface Face2. I don't know if you found out. Class Test includes two methods, one is show, one show1, and one can not be less, if less one, reported fatal error. The test1 in Show1 (test1 $test, $num) must have the same class test1as the name of the root inheriting classes. If it is not the same, fatal error will also be reported. What if an interface is inherited by more than one class and the class name is different? It's going to be self.

Four, one interface multiple inheritance

  1. <?php
  2. Interface Comparable {
  3. function compare (self $compare);
  4. }
  5. Class String implements comparable {
  6. private $string;
  7. function __construct ($string) {
  8. $this->string = $string;
  9. }
  10. function compare (self $compare) {
  11. if ($this->string = = $compare->string) {
  12. return $this->string." ==". $compare->string.  "<br>";
  13. }else{
  14. return $this->string."! =". $compare->string.  "<br>";
  15. }
  16. }
  17. }
  18. Class Integer implements comparable {
  19. private $integer;
  20. function __construct ($int) {
  21. $this->integer = $int;
  22. }
  23. function compare (self $compare) {
  24. if ($this->integer = = $compare->integer) {
  25. return $this->integer." ==". $compare->integer.  "<br>";
  26. }else{
  27. return $this->integer."! =". $compare->integer.  "<br>";
  28. }
  29. }
  30. }
  31. $first _int = new Integer (3);
  32. $second _int = new Integer (4);
  33. $first _string = new String ("foo");
  34. $second _string = new String ("bar");
  35. echo $first _int->compare ($second _int); //3!=4
  36. echo $first _int->compare ($first _int); //3==3
  37. echo $first _string->compare ($second _string); //Foo!=bar
  38. echo $first _string->compare ($second _int); //Critical error
  39. ?>

Note: As can be seen from the above example, an interface can be inherited by multiple classes, and the class name is different. The same class can be called from one another and cannot be called between different classes. echo $first _string->compare ($second _int); fatal wrong.

PHP interface implements use

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.