Note that there are some explanatory texts on larger screens.

plurals
  1. POProblems with parent / inheritance in PHP
    primarykey
    data
    text
    <p><strong>EDIT: I got this working now. I have updated the code:</strong></p> <p>I've looked at a few examples in here, and they all look logical. But I can't get it to work. I have a class that extends my Data Access Layer (DAL). I would like to call parent class to retrieve DB results. What am I doig wrong?</p> <p>DAL class</p> <pre><code>class DAL { protected $username; // the username for db connect protected $pwd; // the pwd to use when connecting protected $host; // the host to which one connects protected $dbname; // the db to select public $conn; // reference to the db link resource public $db; // result of db_select public $query_result; // the stored result of the last query you ran public function __construct() { $this-&gt;username = ""; $this-&gt;pwd = ""; $this-&gt;host = ""; $this-&gt;dbname = ""; } public function connect() { /* connects to DB here */ } private function query($sql) { /* Executes the query here and stores the result in $this-&gt;query_result */ } public function getAllCountries() { $sql =" SELECT id, name FROM country"; //Process query $this-&gt;query($sql); if($this-&gt;query_result) return $this-&gt;query_result; } } </code></pre> <p>And this is my other class</p> <pre><code>class myOtherClass extends DAL{ public function __construct() { parent::__construct(); parent::connect(); } public function getCountryListBox() { $result = parent::getAllCountries(); if($result) { $selectbox = "&lt;select id='countryListBox' name='countryListBox'&gt;"; while ($row = mysql_fetch_array($result)) { $selectbox .= "&lt;option value='".($row['id'])."'&gt;".($row['name'])."&lt;/option&gt;"; } $selectbox .= "&lt;/select&gt;"; } else $selectbox = "Countries could not be retrievd from database."; return $selectbox; } } </code></pre> <p>This is the code in my template:</p> <pre><code>$sl = new myOtherClass(); echo '&lt;form id="label_data_form"&gt;'; $sl-&gt;getCountryListBox(); echo '&lt;/form&gt;'; </code></pre>
    singulars
    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.
 

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