garry27 Posted October 24, 2008 Share Posted October 24, 2008 I have a LISTING class which I used to display a list of gigs up until recently. Having reorganised the class to so that I can make it a superclass for all listing types, I then created a GIG_LISTING class and transfered the methods that apply to this class across. This is something which I have done a few times previously without a problem. Unlike before, this time I've written the superclass so that child classes use the __constructor class of the parent class. I can reference variable which are set in the constructor class fine. The problem rises when I assign the return of a method (which is unique to the parent class) to an attribute inside the construcor, it always returns as NULL. Any idea why this is occuring? Thanks. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted October 24, 2008 Share Posted October 24, 2008 i don't completely understand what you mean. can you post a code sample? Quote Link to comment Share on other sites More sharing options...
discomatt Posted October 24, 2008 Share Posted October 24, 2008 This may help Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required. Quote Link to comment Share on other sites More sharing options...
garry27 Posted October 24, 2008 Author Share Posted October 24, 2008 I'm not declaring a constructor in the child- I want it to inherit it from the parent. I've done some more checks and I can access at least a few methods from the parent class via the parent constructor, via the child class. But not this method: private function assignSortByValue() { ##assigns sortBy value from 'sortBy' GET param if name in $this->columns else assigns first value in $this->columns if($_GET['sortBy']) { while($column = current($this->columns)) { if($_GET['sortBy'] == $column) { $this->sortByValue = $column; } next($this->columns); } } else { $this->sortByValue = current($this->columns); } } which is called like this: function __construct($columns...) { //used for defining db columns and secondary, table columns $this->columns = $columns; Quote Link to comment Share on other sites More sharing options...
garry27 Posted October 24, 2008 Author Share Posted October 24, 2008 If I do this: function __construct($columns,...) { $this->columns = $columns; $this->assignSortByValue(); var_dump($this->sortByValue); ... .. I get the right value but if I try and access $this->sortByValue inside the child, I get a NULL value. Why's this? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted October 24, 2008 Share Posted October 24, 2008 i don't see any problems. can you post the entire code for both classes? Quote Link to comment Share on other sites More sharing options...
garry27 Posted October 24, 2008 Author Share Posted October 24, 2008 Ok, here's goes: <?php //echos a data table with headers. includes other class class LISTING { //table presentation public $columns; // arr(GET value=>Db value...) - defined on new object public $tdColl; public $tblAttr; public $trAttr=''; public $tblNum = 1; //data presentation - defined in constructor public $resultsPerPage; public $numPages; public $maxPages; public $currPage; private $sortByValue; public $filterBy; //GET name i.e. 'County' private $filterByGetValue; //current filter GET value i.e. 'CountyDurham' public $filterByDbValue; //current filter DB value i.e. 'County Durham' public $sqlFilterBy; public $firstRecordOnPage; function __construct($columns,$tblAttr,$resultsPerPage,$maxPages,$filterBy,$filterByDbValues) { //used for defining db columns and secondary, table columns $this->columns = $columns; $this->tblAttr = $tblAttr; $this->resultsPerPage = $resultsPerPage; $this->maxPages = $maxPages; $this->filterBy = $filterBy; $this->assignSortByValue(); $this->assignFilterByParams($filterByDbValues); #set current page number if(!$_GET['Page']) { $this->currPage = 1; } else { $this->currPage = $_GET['Page']; } $numRecords = $this->getNumRecords(); ###get number of pages $x = $numRecords / $this->resultsPerPage; if(is_float($x)) { $this->numPages = $x + 1; } else { $this->numPages = $x; } #get first and last record numbers $this->firstRecordOnPage = $this->currPage * $this->resultsPerPage - $this->resultsPerPage; } //fixed private function assignFilterByParams($filterValues) { ##returns filter value for $this->filterBy GET param while($column = current($filterValues)) { if($_GET[$this->filterBy] == $column) { $this->filterByDbValue = key($filterValues); $this->filterByGetValue = $column; } next($filterValues); } return; } //fixed private function assignSortByValue() { ##assigns sortBy value from 'sortBy' GET param if name in $this->columns else assigns first value in $this->columns if($_GET['sortBy']) { while($column = current($this->columns)) { if($_GET['sortBy'] == $column) { $this->sortByValue = $column; } next($this->columns); } } else { $this->sortByValue = current($this->columns); } return; } function getNumRecords() { $LDM = new LISTING_DATA_MAPPER; if($_GET[$this->filterBy]) { $this->filterBySql = "AND GA_Town.county = '".$this->filterByDbValue."'"; return $LDM->fetchNumGigsByCounty($this->filterByDbValue); } else{ return $LDM->fetchNumGigsByRegion(); } } function getDbListingsObjects() { $LDM = new LISTING_DATA_MAPPER(); $dbListingsObjects = $LDM->fetchGigListings($this->filterBySql,$this->sortByValue, $this->firstRecordOnPage,$this->resultsPerPage); return $dbListingsObjects; } //fixed function getListings() { return "<table ".$this->tblAttr.">" .$this->getListingsThead() .$this->getListingsTbody() ."\n </table>\n" .$this->getPageNumbers(); } function getListingsTHead() { $i = 1; $columns = array_flip($this->columns); unset($columns['endTime']); //no longer required while($txt = current($columns)) { $thCells .= "\n <th id='gigsTd$i'><a class='sortHeaderLink' href='".$_SERVER['PHP_SELF']. "?".$this->filterBy."=".$this->filterByGetValue. "&sortBy=".key($columns)."'>$txt</a></th>"; $i++; next($columns); } return "\n <thead> <tr>".$thCells."</tr> \n </thead>"; } function getListingsTbody() { $listingsObjects = $this->getDbListingsObjects(); while($result = $listingsObjects->fetch_object()) { $cells = null; $columns = $this->columns; unset($columns['End']); //remove end column foreach($columns as $key=>$val) { if($val == 'date') { $timestamp = strtotime($result->$val); $result->$val = date("D, j M" ,$timestamp); } elseif($val == 'entryFee') { if(empty($result->$val)) { $result->$val = 'Free'; } } elseif($val == 'startTime') { //merge start/end into one column $result->$val = $result->$val . '-' . $result->endTime; } elseif(!$result->$val) { $result->$val = 'N/A'; } $cells .= "\n <td>" .$result->$val. "</td>"; } $rows .= "\n <tr". $this->trAttr.">" .$cells . "</tr>"; } return "<tbody>".$rows."</tbody>"; } //fixed private function getPageNumbers() { ###build pages hyperlinks for($i=1; $i<=$this->numPages; $i++ ) { //assign bold style to current page if($i == $this->currPage){ $listContent = "<li style='font-weight:bold;'>$i</li>"; } else{ $listContent = "<a href='/Browse-Gigs.php?".$this->filterBy."=".$this->filterByGetValue."&Page=".$i. "&sortBy=".$this->sortByValue."' title='Gig Results Page $i link'>$i</a>"; } $numbers .= "<li>$listContent</li>"; } return "<div id='pageNumbers'> <ul>" .$numbers ."</ul> </div>"; } } ?> <?php class GIG_LISTING extends LISTING { function getNumRecords() { $LDM = new LISTING_DATA_MAPPER; if($_GET[$this->filterBy]) { $this->filterBySql = "AND GA_Town.county = '".$this->filterByDbValue."'"; return $LDM->fetchNumGigsByCounty($this->filterByDbValue); } else{ return $LDM->fetchNumGigsByRegion(); } } function getDbListingsObjects() { $LDM = new LISTING_DATA_MAPPER(); $dbListingsObjects = $LDM->fetchGigListings($this->filterBySql,$this->sortByValue, $this->firstRecordOnPage,$this->resultsPerPage); // var_dump($this->filterBySql); var_dump($this->sortByValue); //var_dump($this->firstRecordOnPage); //var_dump($this->resultsPerPage); return $dbListingsObjects; } function getListingsTHead() { $i = 1; $columns = array_flip($this->columns); unset($columns['endTime']); //no longer required while($txt = current($columns)) { $thCells .= "\n <th id='gigsTd$i'><a class='sortHeaderLink' href='".$_SERVER['PHP_SELF']. "?".$this->filterBy."=".$this->filterByGetValue. "&sortBy=".key($columns)."'>$txt</a></th>"; $i++; next($columns); } return "\n <thead> <tr>".$thCells."</tr> \n </thead>"; } function getListingsTbody() { $listingsObjects = $this->getDbListingsObjects(); while($result = $listingsObjects->fetch_object()) { $cells = null; $columns = $this->columns; unset($columns['End']); //remove end column foreach($columns as $key=>$val) { if($val == 'date') { $timestamp = strtotime($result->$val); $result->$val = date("D, j M" ,$timestamp); } elseif($val == 'entryFee') { if(empty($result->$val)) { $result->$val = 'Free'; } } elseif($val == 'startTime') { //merge start/end into one column $result->$val = $result->$val . '-' . $result->endTime; } elseif(!$result->$val) { $result->$val = 'N/A'; } $cells .= "\n <td>" .$result->$val. "</td>"; } $rows .= "\n <tr". $this->trAttr.">" .$cells . "</tr>"; } return "<tbody>".$rows."</tbody>"; } } ?> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted October 24, 2008 Share Posted October 24, 2008 ah, in the parent class you have: private $sortByValue; which will block the child class from being able to access it. use 'protected' instead, which will block scripts from accessing it, but child classes can still use it. protected $sortByValue; Quote Link to comment Share on other sites More sharing options...
garry27 Posted October 24, 2008 Author Share Posted October 24, 2008 Ye, I just saw it before! I don't know why it doesn't throw back a error or a warning though, like they do when you try and access a method that's out of 'scope'. Thanks for your help!!!!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.