开发者

PHP extended class not overriding function?

On Class A I have this:

protected function createTempTable()
{
    $qry = '
        CREATE TABLE  `'.$this->temp_table.'` (
         `style` VARCHAR( 255 ) NOT NULL ,
         `description` VARCHAR( 255 ) NOT NULL ,
         `metal` VARCHAR( 255 ) NOT NULL ,
      开发者_如何学JAVA   `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);
}

Class B extends class A and has this:

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 does not actually call createTempTable it lets it's super class ClassA call it.

So in theory when I create a new ClassB class, it's super class calls createTempTable() which should use ClassB's override version of the function. However it does not, it still uses ClassA's version. I confirmed this by doing a SHOW CREATE TABLE from inside ClassB. I expected to have a syn10 column instead I had a description column.

Why is this?

edit:

Here is the code from class A that calls the createTempTable function:

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;
}

Here is how ClassA starts out:

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

This is how classB starts out:

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

And this is how classB is initiated:

$s = new MothersRingsAdvancedCsvFeed(2,3,4,5,1);
echo $s->processPriceSheet('mothers_rings.csv');


EDIT: I notice that the second class doesn't change the value for $temp_table. You may have a collision here: is it possible that some other AdvancedCsvFeed is creating the MySQL table csv_tmp first, and the call to $s->processPriceSheet fails to create the table because it already exists? Try having the constructor for MothersRingsAdvancedCsvFeed set

$this->temp_table = 'mothers_rings_csv_tmp';

ORIGINAL:

There must be something else at play here. Consider the following code:

<?php

class TestOne {
  public function foo() {
    echo "TestOne's foo.";
  }
  public function bar() {
    $this->foo();
    echo " TestOne's bar.";
  }
}
class testTwo extends TestOne {
  public function foo() {
    echo "TestTwo's foo.";
  }
}

$one = new TestOne();
$two = new TestTwo();

$one->foo();
$two->foo();

$one->bar();
$two->bar();

When I run the above, I get the output:

TestOne's foo.TestTwo's foo.TestOne's foo. TestOne's bar.TestTwo's foo. TestOne's bar.

So, there must be something else affecting your code. Can you share a bit more of the code, specifically what calls createTempTable()?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜