Based on an earlier thread (PDO: Am I missing the concept completely? ), I’m simply attempting to replace the EXTEND of my database manager with passing the object as an argument. Apparently whatever object I am passing does not contain the “prepare” function.
(The other change I made from the original db was to change the class and variable name from $db to $dbManager)
Here is the class that sets up the dbManager object
<?php
Class lessons_template_dbManager {
Protected $dbManager = null;
Protected $trace = false;
Public Function __Construct() { // assure connection
if($this->trace) {
echo("<br />lessons_template_dbmanager->Construct");
}
if(isset($this->dbManager)) {
if($this->trace) {
echo("<br />lessons_template_dbmanager->db is set");
}
return true;
}
if($this->trace) {
echo("<br />lessons_template_db->dbmanager is NOT set");
}
$currentHost = pathinfo($_SERVER['REMOTE_ADDR'],PATHINFO_BASENAME);
if($currentHost == "127.0.0.1"){ // no place like home
$theUsername = "root";
$thePassword = "";
} else {
--> detail omitted <--
}
try {
$this->dbManager = new PDO('mysql:host=localhost;dbname=lessons;charset=UTF-8', $theUsername, $thePassword);
$this->dbManager->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->dbManager->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch(PDOException $ex) {
echo("<p style='font-weight: bold; color: red; font-size: larger;'><br /><br /><br />A serious error occurred, contact programmer who will need the following detail<br /></p>");
echo("<br />lessons_template_dbManager->Construct<br />");
var_dump($ex);
exit();
}
} // end function
} // end class
?>
Here’s an excerpt class that uses the above passed as an argument
<?php
Class lessons_horse_dbManager {
protected $trace = true;
private $dbManager;
Public Function __Construct($dbManager) {
if($this->trace) {
echo("<br />lessons_horse_dbManager->__Construct");
}
$this->dbManager = $dbManager;
}
Public Function GetHorseList() {
if($this->trace) {
echo("<br />lessons_horse_dbManager->GetHorseList");
}
// dispensing with try-catch
$stmt = $this->dbManager->prepare("SELECT name FROM horses ORDER BY name");
$stmt->execute();
$nameList = array();
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$nameList[] = $row['name'];
} // end while
return($nameList);
} // end function
...
And here’s the calling sequence from the instantiating class
Class lessons_horse_Form
extends lessons_template_Base{
...
Function BuildContent(){
...
$dbManager = new lessons_template_dbManager();
$horseObj = new lessons_horse_dbManager($dbManager);
$horseValues = $horseObj->GetHorseList();
The message I get is
Fatal error: Call to undefined method lessons_template_dbManager::prepare() in C:\xampp\htdocs\lessons\horse\dbmanager.php on line 18
[I have “no icon” selected, the message should show as …dbManager colon colon prepare()]
Where line 18 is ‘$stmt = $this->dbManager->prepare(“SELECT name FROM horses ORDER BY name”);’ from class lessons_horse_dbManager. (And again, this worked when I extended the original lessons_template_db class).
Any ideas?
Regards,
grNadpa