Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP subclass can't access public variable set by parent
    primarykey
    data
    text
    <p>I'm very new to PHP and OOP in general. I'm using codeigniter for a framework, and am currently attempting to build a class 'BuildLinks' that will redirect the user to the correct link based on what URL they landed on. </p> <p>The controller passes the right variables to the class, while the function build_afflink() selects what class to call next based on the var1</p> <p>The controller:</p> <pre><code> function out($var1, $var2) { // redirect to link after parsing data $debug='1'; $params = array('var1'=&gt;$var1, 'var2'=&gt;$var2); $this-&gt;load-&gt;library('BuildLinks', $params); if ($debug=='0'){ $redirect = $this-&gt;buildlinks-&gt;build_afflink(); redirect($redirect, 'location', 301); } else { var_dump($this-&gt;buildlinks-&gt;build_afflink()); } } </code></pre> <p>The class BuildLinks is a work in progress... but it is extended by all of the other sites I need to support. </p> <p>BuildLinks class:</p> <pre><code> class BuildLinks { public $var1; public $var2; public $link; function __construct($params) { //populate up inititial variables from $params array (passed from controller) $this-&gt;var1 = (string)$params['var1']; $this-&gt;var2 = (string)$params['var2']; echo __class__ . ' loaded....' . 'var1: '.$this-&gt;var1 . ' var2: ' .$this-&gt;var2. '&lt;br/&gt;'; } public function get_var1() { return $this-&gt;var1; } public function set_var1($var1) { $this-&gt;var1 = $var1; } public function get_var2() { return $this-&gt;var2; } public function set_var2($var2) { $this-&gt;var2 = $var2; } function build_thelink() { switch ($this-&gt;var1) { case 'amazon': //echo 'Amazon is our vendor.&lt;br&gt;'; $newobj = new Amazon; // Amazon subclass returns the correct affiliate link return $newobj-&gt;affiliate_link(); break; case 'ebay': $newobj = new Ebay; //ebay subclass however, cannot access the public var var1, it returns a null value for $this-&gt;var1 return $newobj-&gt;affiliate_link(); break; } } } </code></pre> <p>Essentially, when I make a new Ebay object, it can't access any of the public variables from the parent class BuildLinks. What am I doing wrong?</p> <p>In this example, I have it construct the initial variables and echo back out some information for debugging.</p> <p><strong>EDIT:</strong> If I change the __construct to read:</p> <pre><code> function __construct($params) { //populate up inititial variables from $params array (passed from controller) $this-&gt;var1 = (string)$params['var1']; $this-&gt;var2 = (string)$params['var2']; echo __class__ . ' loaded....' . 'var1: '.$this-&gt;var1. ' var2: ' .$this-&gt;var2 . '&lt;br/&gt;'; var_dump(get_class_vars(get_class($this))); } </code></pre> <p>Then I get this as an output:</p> <pre><code> BuildLinks loaded....var1: ebay var2: somedata array 'var1' =&gt; null 'var2' =&gt; null 'link' =&gt; null </code></pre> <p>The following works fine:</p> <pre><code>$newobj = new Amazon; return $newobj-&gt;affiliate_link(); </code></pre> <p>This does not, but the classes are almost identical...</p> <pre><code>$newobj = new Ebay; return $newobj-&gt;affiliate_link(); </code></pre> <p>Here is the ebay class:</p> <pre><code> class Ebay extends BuildLinks { private $res; //TODO: language/multiple site support //public $locale; function __construct() { //echo __class__ . ' loaded....' . 'vendor: '.$this-&gt;vendor . ' isbn: ' .$this-&gt;isbn . '&lt;br/&gt;'; } function add_to_cart_button($isbn, $locale) { } function affiliate_link() { $this-&gt;get_info(); return $this-&gt;link; } // Load $this-&gt;api_call() and return Amazon SimpleXML response object, load variables function get_info() { $apicall = $this-&gt;api_call(); //build variables foreach ($apicall-&gt;searchResult-&gt;item as $item) { $this-&gt;link = (string )$item-&gt;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-&gt;ack == "Success") { $this-&gt;res = $res; return $res; } else { echo 'api_call() unsuccessful'; } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload