2021/07/31/a code mystery: Difference between revisions

From Woozle Writes Code
Jump to navigation Jump to search
(Created page with "==Part 1== <syntaxhighlight lang=php> namespace ferret\data\bank; [...] class cIOUnitSimple extends cIOUnit { </syntaxhighlight> ==Part 2== <syntaxhighlight lang=php> namesp...")
 
Line 29: Line 29:
==Conundrum==
==Conundrum==
It doesn't make sense, because the default IOUnit class retrieved (into $sClass, in part 2), <code>ferret\data\bank\cIOUnitSimple</code>, is in fact directly descended from <code>ferret\data\bank\cIOUnit</code> (in part 1). I can only think I'm reading the definition of <code>is_a()</code> wrong somehow.
It doesn't make sense, because the default IOUnit class retrieved (into $sClass, in part 2), <code>ferret\data\bank\cIOUnitSimple</code>, is in fact directly descended from <code>ferret\data\bank\cIOUnit</code> (in part 1). I can only think I'm reading the definition of <code>is_a()</code> wrong somehow.
Tenatatively: [https://www.php.net/manual/en/function.is-a <code>is_a()</code>] is messed up; [https://www.php.net/manual/en/function.is-subclass-of.php <code>use is_subclass_of()</code>] instead.

Revision as of 23:38, 31 July 2021

Part 1

namespace ferret\data\bank;

[...]

class cIOUnitSimple extends cIOUnit {

Part 2

namespace ferret\data;
use ferret\data\bank\cIOUnit;
use ferret\data\bank\cIOUnitSimple;

[...]

$sClass = $this->DefaultIOUnitClass();
if (is_a($sClass,cIOUnit::class)) {
    // create new I/O Unit object
    $oIOUnit = new $sClass($this,$ouNative);
} else {
    $s = "Default IOUnit class is [$sClass], which is not a cIOUnit.";
    $e = new \ferret\except\cInternal($s);
    throw $e;
}

Part 3 - output

Internal ferret panic: Default IOUnit class is [ferret\data\bank\cIOUnitSimple], which is not a cIOUnit.

Conundrum

It doesn't make sense, because the default IOUnit class retrieved (into $sClass, in part 2), ferret\data\bank\cIOUnitSimple, is in fact directly descended from ferret\data\bank\cIOUnit (in part 1). I can only think I'm reading the definition of is_a() wrong somehow.

Tenatatively: is_a() is messed up; use is_subclass_of() instead.