Difference between revisions of "PHP/inheritance"

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
(Created page with "==About== PHP includes a few bits of functionality for checking whether an object or class inherits from another classoid (class, interface, or trait). The rules for this func...")
 
Line 4: Line 4:
 
===Class Inheritance Functions Testing===
 
===Class Inheritance Functions Testing===
 
Here's a truth-table from PHP 8.2.10:
 
Here's a truth-table from PHP 8.2.10:
{| class="wikitable sortable"
+
{| class='wikitable sortable'
 
! arguments || B instanceof A || is_a(B,A,FALSE) || is_a(B,A,TRUE) || is_subclass_of(B,A,FALSE) || is_subclass_of(B,A,TRUE)  
 
! arguments || B instanceof A || is_a(B,A,FALSE) || is_a(B,A,TRUE) || is_subclass_of(B,A,FALSE) || is_subclass_of(B,A,TRUE)  
 
|-
 
|-
Line 63: Line 63:
 
| Y
 
| Y
 
|}
 
|}
 +
 +
Key:
 +
* cX = class X
 +
* iX = interface X
 +
* oX = object of class X
 +
* tX = trait X

Revision as of 12:39, 5 April 2024

About

PHP includes a few bits of functionality for checking whether an object or class inherits from another classoid (class, interface, or trait). The rules for this functionality are not always clearly explained.

Class Inheritance Functions Testing

Here's a truth-table from PHP 8.2.10:

arguments B instanceof A is_a(B,A,FALSE) is_a(B,A,TRUE) is_subclass_of(B,A,FALSE) is_subclass_of(B,A,TRUE)
cA, cA - - Y - -
cB, cA - - Y - Y
oB, cA Y Y Y Y Y
cA, iA - - Y - Y
cB, iA - - Y - Y
cB, iB - - Y - Y
oB, iA Y Y Y Y Y
oB, iB Y Y Y Y Y

Key:

  • cX = class X
  • iX = interface X
  • oX = object of class X
  • tX = trait X