Difference between revisions of "PHP/interface"
< PHP
Jump to navigation
Jump to search
Line 15: | Line 15: | ||
* official: [https://www.php.net/manual/en/language.oop5.interfaces.php interfaces] | * official: [https://www.php.net/manual/en/language.oop5.interfaces.php interfaces] | ||
* '''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== | ||
+ | I thought I had derived this rule, but then when I went to test it ''without'' the backslash again, it was working fine: | ||
+ | <blockquote> | ||
+ | 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: | ||
+ | <source lang=php>class fcrUserAcct_admin extends fcrUserAcct implements fiLinkableRecord, \ferreteria\data\ifEditableRecord, fiEventAware {</source> | ||
+ | ...where <code>ifEditableRecord</code> is being declared inside the <code>ferreteria\data</code> namespace. | ||
+ | |||
+ | For all other uses, the backslash is optional or forbidden. | ||
+ | </blockquote> | ||
+ | Perhaps there was some kind of code-caching error? If it happens again, try the backslash. |
Revision as of 01:27, 13 January 2020
About
In PHP, an interface is a set of public method-functions which can be required in either of two circumstances:
- in the declaration of a class
- This works almost identically to declaring the interface's methods as abstract functions in a trait or parent class.
- the type of a function argument or return value
- Advantages over declaring the object as a class:
- narrows down (more closely specifies) the function's expectations of a passed or returned object
- does not tie the object to a specific class hierarchy
- Advantages over declaring the object as a class:
Predefined
- instanceof works with both classes and interfaces
- interface_exists() returns TRUE if the interface has been declared in code that has executed.
Links
- official: interfaces
- 2018-01-13 PHP Interfaces Explained
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 theferreteria\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.