PHP/inheritance/code

from HTYP, the free directory anyone can edit if they can prove to me that they're not a spambot
< PHP‎ | inheritance
Revision as of 13:26, 5 April 2024 by Woozle (talk | contribs) (Created page with "<syntaxhighlight lang=php> <?php /** * PURPOSE: The idea is to produce a handy reference-matrix of what the various class-inheritance functions/operators report * under var...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
<?php
/**
 * PURPOSE: The idea is to produce a handy reference-matrix of what the various class-inheritance functions/operators report
 *  under various conditions.
 *  * vertical axis: two entities being compared
 *  * horizontal axis: instanceof, is_a(,,FALSE), is_a(,,TRUE), is_subclass_of(,,FALSE), is_subclass_of(,,TRUE)
 * HISTORY:
 *  2024-04-04 created because the documentation is maddeningly unclear on some key points and doesn't mention others at all
 *  2024-04-05 more or less finalized
 */

// PLAINTEXT
/*
define('CRLF',"\n");
define('TABLE_OPEN','');
define('TABLE_SHUT','');
define('ROW_OPEN','');
define('ROW_SHUT',"\n");
define('CELL_OPEN',' ');
define('CELL_SHUT',' ');

function Cell(string $s) : string { return CELL_OPEN.str_pad($s,8,' ').CELL_SHUT; }
function WholeRow(string $s) : string { return ROW_OPEN.CELL_OPEN.$s.CELL_SHUT.ROW_SHUT; }
*/
// HTML

// to be written

// WIKITEXT

define('CRLF',"\n");
define('TABLE_OPEN',"{| class='wikitable sortable'\n! 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) \n");
define('TABLE_SHUT',"|}\n");
define('ROW_OPEN',"|-\n");
define('ROW_SHUT',"");
define('CELL_OPEN',"| ");
define('CELL_SHUT',"\n");

function Cell(string $s) : string { return CELL_OPEN.$s.CELL_SHUT; }
function WholeRow(string $s) : string { return ROW_OPEN.CELL_OPEN."colspan=6 | $s".CELL_SHUT.ROW_SHUT; }

// ----

interface iA {}
interface iB extends iA {}            // interface B inherits from interface A
trait tA {}
class cA implements iA { use tA; }    // class A inherits from iA and tA
$oA = new cA;                         // object of class A
class cB extends cA implements iB {}  // class B inherits from cA and iB
$oB = new cB;                         // object of class B
class cD {}                           // unrelated class
$oD = new cD;                         // object of class D

function YesNo(bool $b) : string { return $b ? 'Y' : '-'; }

function RenderRow(string $sDesc,$v1,string $v2) : string {
    $s1 = YesNo( $v1 instanceof $v2 );
    $s2 = YesNo( is_a($v1,$v2,FALSE) );
    $s3 = YesNo( is_a($v1,$v2,TRUE) );
    $s4 = YesNo( is_subclass_of($v1,$v2,FALSE) );
    $s5 = YesNo( is_subclass_of($v1,$v2,TRUE) );

    return ROW_OPEN
      .Cell($sDesc)
      .CELL_OPEN.$s1.CELL_SHUT
      .CELL_OPEN.$s2.CELL_SHUT
      .CELL_OPEN.$s3.CELL_SHUT
      .CELL_OPEN.$s4.CELL_SHUT
      .CELL_OPEN.$s5.CELL_SHUT
      .ROW_SHUT
      ;
}

echo 'Class Inheritance Functions Testing'.CRLF;

echo TABLE_OPEN;

echo WholeRow('compare things to cA');
echo RenderRow('cA, cA',cA::class,cA::class);
echo RenderRow('cB, cA',cB::class,cA::class);
echo RenderRow('$oA, cA',$oA,     cA::class);
echo RenderRow('$oB, cA',$oB,     cA::class);

echo WholeRow('compare things to iA');
echo RenderRow('cA, iA',cA::class,iA::class);
echo RenderRow('$oA, iA',$oA,     iA::class);
echo RenderRow('cB, iA',cB::class,iA::class);
echo RenderRow('$oB, iA',$oB,     iA::class);

echo WholeRow('compare things to iB');
echo RenderRow('cB, iB',cB::class,iB::class);
echo RenderRow('$oB, iB',$oB,     iB::class);

echo WholeRow('compare things to tA');
echo RenderRow('cA, tA',cA::class,tA::class);
echo RenderRow('cB, tA',cB::class,tA::class);
echo RenderRow('$oB, tA',$oB,tA::class);

echo WholeRow('compare xD to things (should always be FALSE)');
echo RenderRow('cD, cA',cD::class,cA::class);
echo RenderRow('$oD, cA',$oD,cA::class);

echo TABLE_SHUT;