在A级我有这个:
protected function createTempTable()
{
$qry = '
CREATE TABLE `'.$this->temp_table.'` (
`style` VARCHAR( 255 ) NOT NULL ,
`description` VARCHAR( 255 ) NOT NULL ,
`metal` VARCHAR( 255 ) NOT NULL ,
`available` VARCHAR( 255 ) NOT NULL ,
`center_stone` VARCHAR( 255 ) NOT NULL ,
`total_weight` VARCHAR( 255 ) NOT NULL ,
`price` DECIMAL( 10, 2 ) NOT NULL ,
PRIMARY KEY ( `style` )
) ENGINE = MYISAM ;
';
$pdo = PDOManager::getConnection();
$sth = $pdo->query($qry);
}
B类扩展了A类并具有:
protected function createTempTable()
{
$qry = '
CREATE TABLE `'.$this->temp_table.'` (
`style` VARCHAR( 255 ) NOT NULL ,
`syn10` DECIMAL( 10, 2 ) NOT NULL ,
`gen10` DECIMAL( 10, 2 ) NOT NULL ,
`syn14` DECIMAL( 10, 2 ) NOT NULL ,
`gen14` DECIMAL( 10, 2 ) NOT NULL ,
PRIMARY KEY ( `style` )
) ENGINE = MYISAM ;
';
$pdo = PDOManager::getConnection();
$sth = $pdo->query($qry);
}
ClassB实际上并没有调用createTempTable它让它的超类ClassA调用它.
所以理论上当我创建一个新的ClassB类时,它的超类调用createTempTable(),它应该使用ClassB的函数覆盖版本.但事实并非如此,它仍然使用ClassA的版本.我通过从ClassB内部执行SHOW CREATE TABLE来确认这一点.我希望有一个syn10列,而不是我有一个描述列.
为什么是这样?
编辑:
以下是调用createTempTable函数的A类代码:
public function processPriceSheet ($file, $test = false)
{
if(!file_exists($file))
{
die('The file "'.$file.'" does not exist.');
}
$fp = fopen($file,'r');
$this->createTempTable();
while (!feof($fp))
{
$row = fgetcsv($fp);
$this->processLine($row);
}
fclose($fp);
$products_updates = $this->massUpdate();
$this->findMissingFromDB();
$this->findMissingFromCSV();
return $products_updates;
}
以下是ClassA的开始:
class AdvancedCsvFeed
{
protected $vendor_prefix;
protected $style_column;
protected $price_column;
protected $price_multiplier;
protected $instock_column;
protected $temp_table = 'csv_tmp';
public function __construct($price_column, $style_column, $vendor_prefix = '', $price_multiplier = 1, $instock_column = 0)
{
$this->vendor_prefix = $vendor_prefix;
$this->price_column = $price_column;
$this->style_column = $style_column;
$this->price_multiplier = $price_multiplier;
$this->instock_column = $instock_column;
}
... other functions
这就是classB的开始:
class MothersRingsAdvancedCsvFeed extends AdvancedCsvFeed
{
private $syn10_price_column;
private $gen10_price_column;
private $syn14_price_column;
private $gen14_price_column;
public function __construct($syn10_price_column, $gen10_price_column, $syn14_price_column, $gen14_price_column, $style_column, $price_multiplier = 3)
{
$this->syn10_price_column = $syn10_price_column;
$this->gen10_price_column = $gen10_price_column;
$this->syn14_price_column = $syn14_price_column;
$this->gen14_price_column = $gen14_price_column;
$this->style_column = $style_column;
$this->price_multiplier = $price_multiplier;
}
... other functions
这就是classB的启动方式:
$s = new MothersRingsAdvancedCsvFeed(2,3,4,5,1);
echo $s->processPriceSheet('mothers_rings.csv');