PHP扩展类不具有覆盖功能吗? [英] PHP extended class not overriding function?

查看:52
本文介绍了PHP扩展类不具有覆盖功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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类并具有:

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实际上并没有调用createTempTable,而是让它作为超类ClassA对其进行调用.

ClassB does not actually call createTempTable it lets it's super class ClassA call it.

所以从理论上讲,当我创建一个新的ClassB类时,它是调用createTempTable()的超类,该超类应使用ClassB的该函数的替代版本.但是,它没有,它仍然使用ClassA的版本.我通过在ClassB内部执行SHOW CREATE TABLE确认了这一点.我本来希望有一个syn10列,而不是一个description列.

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.

这是为什么?

这是A类中调用createTempTable函数的代码:

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

这是ClassA入门的方式:

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

这是classB的开始方式:

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

这是classB的启动方式:

And this is how classB is initiated:

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

推荐答案

编辑:我注意到第二个类不会更改$temp_table的值.您可能在这里遇到冲突:是否可能有其他AdvancedCsvFeed首先创建MySQL表csv_tmp,并且对$s->processPriceSheet的调用由于已经存在而无法创建该表?尝试设置MothersRingsAdvancedCsvFeed的构造函数

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

原始:

这里肯定还有其他事情.考虑以下代码:

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的foo.TestTwo的foo.TestOne的foo. TestOne的bar.TestTwo的foo. TestOne的酒吧.

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

因此,必须有其他因素影响您的代码.您是否可以共享更多代码,尤其是调用createTempTable()的代码?

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

这篇关于PHP扩展类不具有覆盖功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆