codice:
<?php
class Component{
public function __construct($what)
{
switch($what)
{
case 1:
{
$this = new Resistance();
break;
}
case 2:
{
$this = new Capacitance();
break;
}
}
}
abstract function getName();
}
//Resistance
class Resistance extends Component
{
function getName()
{
return "i'm a resistor";
}
}
//Capacitance
class Capacitance extends Component
{
function getName()
{
return "i'm a capacitor";
}
}
////////////////////////////////////////////////////
$comp = new Component(1);
echo $comp->getName();
?>
Ecco ciò che ho intenzione di fare. ovviamente ricevo un errore quando tento di riassegnare $this, ma questa è l'idea di ciò che vorrei fare.
come potrei implementare ciò?