我对PHP和OOP很新。我正在使用codeigniter作为框架,我目前正在尝试构建一个类“BuildLinks”,它将根据用户登陆的URL将用户重定向到正确的链接。
控制器将正确的变量传递给类,而函数build_afflink()根据var1选择接下来要调用的类
控制器:
function out($var1, $var2)
{
// redirect to link after parsing data
$debug='1';
$params = array('var1'=>$var1, 'var2'=>$var2);
$this->load->library('BuildLinks', $params);
if ($debug=='0'){
$redirect = $this->buildlinks->build_afflink();
redirect($redirect, 'location', 301);
}
else {
var_dump($this->buildlinks->build_afflink());
}
}
BuildLinks类正在进行中......但是我需要支持的所有其他站点都会扩展它。
BuildLinks类:
class BuildLinks
{
public $var1;
public $var2;
public $link;
function __construct($params)
{
//populate up inititial variables from $params array (passed from controller)
$this->var1 = (string)$params['var1'];
$this->var2 = (string)$params['var2'];
echo __class__ . ' loaded....' . 'var1: '.$this->var1 . ' var2: ' .$this->var2. '<br/>';
}
public function get_var1()
{
return $this->var1;
}
public function set_var1($var1)
{
$this->var1 = $var1;
}
public function get_var2()
{
return $this->var2;
}
public function set_var2($var2)
{
$this->var2 = $var2;
}
function build_thelink()
{
switch ($this->var1) {
case 'amazon':
//echo 'Amazon is our vendor.<br>';
$newobj = new Amazon;
// Amazon subclass returns the correct affiliate link
return $newobj->affiliate_link();
break;
case 'ebay':
$newobj = new Ebay;
//ebay subclass however, cannot access the public var var1, it returns a null value for $this->var1
return $newobj->affiliate_link();
break;
}
}
}
基本上,当我创建一个新的Ebay对象时,它无法访问父类BuildLinks中的任何公共变量。我做错了什么?
在这个例子中,我构建了初始变量并回显了一些调试信息。
编辑:如果我将__construct更改为:
function __construct($params)
{
//populate up inititial variables from $params array (passed from controller)
$this->var1 = (string)$params['var1'];
$this->var2 = (string)$params['var2'];
echo __class__ . ' loaded....' . 'var1: '.$this->var1. ' var2: ' .$this->var2 . '<br/>';
var_dump(get_class_vars(get_class($this)));
}
然后我把它作为输出:
BuildLinks loaded....var1: ebay var2: somedata
array
'var1' => null
'var2' => null
'link' => null
以下工作正常:
$newobj = new Amazon;
return $newobj->affiliate_link();
这不是,但课程几乎相同......
$newobj = new Ebay;
return $newobj->affiliate_link();
以下是ebay课程:
class Ebay extends BuildLinks
{
private $res;
//TODO: language/multiple site support
//public $locale;
function __construct()
{
//echo __class__ . ' loaded....' . 'vendor: '.$this->vendor . ' isbn: ' .$this->isbn . '<br/>';
}
function add_to_cart_button($isbn, $locale)
{
}
function affiliate_link()
{
$this->get_info();
return $this->link;
}
// Load $this->api_call() and return Amazon SimpleXML response object, load variables
function get_info()
{
$apicall = $this->api_call();
//build variables
foreach ($apicall->searchResult->item as $item) {
$this->link = (string )$item->viewItemURL;
}
}
// Generate API call and return simplexml object $res
function api_call()
{
//build $apicall here
$res = simplexml_load_file($apicall);
// Check to see if the request was successful, else print an error
if ($res->ack == "Success") {
$this->res = $res;
return $res;
} else {
echo 'api_call() unsuccessful';
}
}
}
答案 0 :(得分:0)
Ebay :: get_info()执行此操作
foreach ($apicall->searchResult->item as $item) {
$this->link = (string )$item->viewItemURL;
}
如果$apicall->searchResult->item
碰巧有几个元素,最后一个是空的,那么你只会获得最后一个空元素。
这是一个疯狂的猜测。
[加入]
你正在覆盖Ebay中的构造函数,所以如果你想要调用BuildLinks的类构造函数,你需要明确地这样做。
答案 1 :(得分:0)
ebay课程的这一部分对我来说没有多大意义:
function api_call()
{
//build $apicall here
$res = simplexml_load_file($apicall);
// Check to see if the request was successful, else print an error
if ($res->ack == "Success") {
$this->res = $res;
return $res;
} else {
echo 'api_call() unsuccessful';
}
}
simplexml_load_file($apicall);
具体不加载任何内容。哪里是$apicall
?如果您调试它,它是否回显“api_call()不成功”?
因为如果不成功,则Ebay::link
将不会被设置,因此return $newobj->affiliate_link();
不会返回任何内容。
为了澄清,$apicall
应该在调用simplexml_load_file($apicall);
之前设置为文件路径