Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ,
         `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');
like image 522
JD Isaacks Avatar asked Jan 20 '11 19:01

JD Isaacks


People also ask

How to override a function in a class PHP?

To override a method, you redefine that method in the child class with the same name, parameters, and return type. The method in the parent class is called overridden method, while the method in the child class is known as the overriding method.

Is method overriding possible in PHP?

Let's explore how overriding works in PHP. Overriding is a concept where the derived class of the base class performs the same action as that of a base class. This overriding can be on methods or classes.

How to create a new instance of a class in PHP?

new ¶ To create an instance of a class, the new keyword must be used. An object will always be created unless the object has a constructor defined that throws an exception on error. Classes should be defined before instantiation (and in some cases this is a requirement).


1 Answers

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()?

like image 53
Josh Avatar answered Oct 14 '22 11:10

Josh