Difference between revisions of "PHP/interface"

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< PHP
Jump to navigation Jump to search
 
Line 16: Line 16:
 
** [https://www.php.net/manual/en/function.interface-exists.php official]
 
** [https://www.php.net/manual/en/function.interface-exists.php official]
 
==Links==
 
==Links==
* official: [https://www.php.net/manual/en/language.oop5.interfaces.php interfaces]
+
===official===
 +
* [https://www.php.net/manual/en/language.oop5.interfaces.php interfaces]
 +
** There are some omissions on this page:
 +
*** "To implement an interface, the {{l/php/keyword|implements}} operator is used. All methods in the interface must be implemented within a class; failure to do so will result in a fatal error." This is not true if the class is declared {{l/php/keyword|abstract}}. In this context, an interface can be thought of as equivalent to a class whose methods are all abstract (and public).
 +
*** There is no mention of whether interfaces can declare member variables. (I ''think'' they cannot, but I haven't tested this yet.)
 +
**** Note that they ''can'' declare (class) constants.
 +
===other===
 
* '''2018-01-13''' [https://daylerees.com/php-interfaces-explained/ PHP Interfaces Explained]
 
* '''2018-01-13''' [https://daylerees.com/php-interfaces-explained/ PHP Interfaces Explained]
 +
 
==Notes==
 
==Notes==
 
I thought I had derived this rule, but then when I went to test it ''without'' the backslash again, it was working fine:
 
I thought I had derived this rule, but then when I went to test it ''without'' the backslash again, it was working fine:

Latest revision as of 13:45, 25 August 2024

About

In PHP, an interface is a set of public method-functions which can be required in either of two circumstances:

  • "implements" in the declaration of a class
    • This works almost identically to (the alternative of) declaring the interface's functions as abstract functions in a trait or parent class.
  • as a type for a function argument or return value
    • Advantages over using a class as a type:
      • narrows down (more closely specifies) the function's expectations of a passed or returned object
      • does not tie the object to a specific class/implementation hierarchy

This last point is one of the primary ways in which interfaces are useful: you may have a set of otherwise-unrelated classes with a set of descendants who need to do a set of things that were not part of their parent classes, and other parts of the code need to be able to recognize them as having this functionality. Having those descendants implement a common interface allows other code to require passed arguments to implement that interface, regardless of class-parentage.

Predefined

Links

official

  • interfaces
    • There are some omissions on this page:
      • "To implement an interface, the implements operator is used. All methods in the interface must be implemented within a class; failure to do so will result in a fatal error." This is not true if the class is declared abstract. In this context, an interface can be thought of as equivalent to a class whose methods are all abstract (and public).
      • There is no mention of whether interfaces can declare member variables. (I think they cannot, but I haven't tested this yet.)
        • Note that they can declare (class) constants.

other

Notes

I thought I had derived this rule, but then when I went to test it without the backslash again, it was working fine:

If an interface is declared within a namespace but used outside of that namespace in am "implements" clause, the full namespace path must be prefixed with a backslash or it won't match:

class fcrUserAcct_admin extends fcrUserAcct implements fiLinkableRecord, \ferreteria\data\ifEditableRecord, fiEventAware {

...where ifEditableRecord is being declared inside the ferreteria\data namespace.

For all other uses, the backslash is optional or forbidden.

Perhaps there was some kind of code-caching error? If it happens again, try the backslash.