Php 在子类问题中设置父类变量
调用Php 在子类问题中设置父类变量,php,Php,调用getLivePath()时,为什么路径中没有包含文件名和getDevPath()方法 <?php class supplierImport { var $_supplierFilename = ''; var $_fileName = ''; var $_livePath = '/var/www/vhosts/myshop.co.uk/httpdocs/_import/'; var $_devPath = '/var/www/web-ecommerc
getLivePath()时,为什么路径中没有包含文件名
和getDevPath()代码>方法
<?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();
示例代码:
http://sandbox.onlinephpfunctions.com/code/435a4b25db44d2c8bb33ff6aa2d96c6db21ef177
您正在扩展基类,因此必须实例化扩展基类的类。然后扩展类将运行其构造函数,正确设置值
<?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();
看起来您正在阅读一个非常旧的PHP版本manual@RiggsFolly是的,我必须使用一些非常旧的phpcode@user1532669哪个版本?$supplierImport=newsupplierimport('birlea')
-这仍然会为您提供一个supplierImport
类的实例。仅仅是在构造函数中使用$birlea=new birlea()
创建birlea
的实例这一事实并没有改变这一点。是的,$birlea
现在包含一个已调用该类构造函数的birlea
实例。但这不会影响存储在$supplierImport
单个位中的内容。
<?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();
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