0

Why isn't the filename being included in the path when calling the getLivePath(); and getDevPath(); methods?

<?php

class supplierImport {
    var $_supplierFilename = '';
    var $_fileName = '';
    var $_livePath = '/var/www/vhosts/myshop.co.uk/httpdocs/_import/';
    var $_devPath = '/var/www/web-ecommerce/www/_import/';


    function supplierImport($supplier){
        switch($supplier){
            case 'birlea';
            $birlea = new birlea();
            break;
            case 'julianbowen';
            $julianbowen = new julianbowen();
            default;
            echo 'Supplier not available';
            break;
        }

    }

    function getLivePath(){
        return $this->_livePath.'/'.$this->_fileName;
    }

    function getDevPath(){
        return $this->_devPath.'/'.$this->_fileName;
    }

}


class birlea extends supplierImport {
    function birlea(){
        $this->_fileName = 'birlea_stock.csv';
    }
}

class julianbowen extends supplierImport {
    function julianbowen(){
        $this->_fileName = 'julianbowen_stock.csv';
    }
}

$supplierImport = new supplierImport('birlea');
echo $supplierImport->getLivePath();
echo $supplierImport->getDevPath();

This is the output I get:

 /var/www/vhosts/myshop.co.uk/httpdocs/_import///var/www/web-ecommerce/www/_import//

Example code:

http://sandbox.onlinephpfunctions.com/code/435a4b25db44d2c8bb33ff6aa2d96c6db21ef177
4
  • 3
    Looks like you have been reading a very old version of the PHP manual Commented Nov 7, 2018 at 10:59
  • @RiggsFolly yes I'm having to work with some very old php code Commented Nov 7, 2018 at 10:59
  • 1
    @user1532669 Which version? Commented Nov 7, 2018 at 11:04
  • $supplierImport = new supplierImport('birlea') - this still gets you an instance of your supplierImport class. The mere fact that inside the constructor you create an instance of birlea with $birlea = new birlea() doesn’t change anything about that. Yes, $birlea now contains a birlea instance for which the constructor of that class has been called. But this does not influence what you got stored in $supplierImport one single bit. Commented Nov 7, 2018 at 11:08

1 Answer 1

1

You are extending the base class so you have to instantiate the class that is extending the base class. Then the extended class will run its constructor setting the values correctly.

<?php

class supplierImport {
    var $_supplierFilename = '';
    var $_fileName = '';
    var $_livePath = '/var/www/vhosts/myshop.co.uk/httpdocs/_import/';
    var $_devPath = '/var/www/web-ecommerce/www/_import/';

    function getLivePath(){
        return $this->_livePath.$this->_fileName;
        // removed unnecesary `.'/'.`
    }

    function getDevPath(){
        return $this->_devPath.$this->_fileName;
        // removed unnecesary `.'/'.`
    }
}


class birlea extends supplierImport {
    function birlea(){
        $this->_fileName = 'birlea_stock.csv';
    }
}

class julianbowen extends supplierImport {
    function julianbowen(){
        $this->_fileName = 'julianbowen_stock.csv';
    }
}

$birlea = new birlea();
echo 'Birlea Live = ' . $birlea->getLivePath();
echo PHP_EOL;
echo 'Birlea Dev = ' . $birlea->getDevPath();
echo PHP_EOL;
$julian = new julianbowen();
echo 'Julian LIVE = ' . $julian->getLivePath();
echo PHP_EOL;
echo 'Julian DEV = ' . $julian->getDevPath();

RESULT

Birlea Live = /var/www/vhosts/myshop.co.uk/httpdocs/_import/birlea_stock.csv
Birlea Dev = /var/www/web-ecommerce/www/_import/birlea_stock.csv
Julian LIVE = /var/www/vhosts/myshop.co.uk/httpdocs/_import/julianbowen_stock.csv
Julian DEV = /var/www/web-ecommerce/www/_import/julianbowen_stock.csv
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.