Hi,
ich hab grad meine DB Connection ausgelagert als singleton, bin aber in der oop in php noch nicht so geübt. Deswegen stoße ich auf folgendes Problem:
habe eine class config:
Eine Class Core:
Und eine Klasse class_Account, in wessen Konstruktor ich die Instanz Core erzeugen und nutzen möchte:
Ordnerstruktur ist die Folgende:
Fehler:
Fatal error: Class 'account\Core' not found
in der Zeile:
Regards,
Sakron
ich hab grad meine DB Connection ausgelagert als singleton, bin aber in der oop in php noch nicht so geübt. Deswegen stoße ich auf folgendes Problem:
habe eine class config:
PHP-Code:
namespace database;
class Config
{
static $confArray;
public static function read($name)
{
return self::$confArray[$name];
}
public static function write($name, $value)
{
self::$confArray[$name] = $value;
}
}
// db
Config::write('db.host', '127.0.0.1');
Config::write('db.port', '3306');
Config::write('db.basename', 'testdb');
Config::write('db.user', 'root');
Config::write('db.password', '');
Eine Class Core:
PHP-Code:
namespace database;
class Core
{
public $dbh; // handle of the db connexion
private static $instance;
private function __construct()
{
// building data source name from config
$dsn = 'mysql:host=' . Config::read('db.host') .
';dbname=' . Config::read('db.basename') .
';port=' . Config::read('db.port') .
';connect_timeout=15';
// getting DB user from config
$user = Config::read('db.user');
// getting DB password from config
$password = Config::read('db.password');
$this->dbh = new PDO($dsn, $user, $password);
}
public static function getInstance()
{
if (!isset(self::$instance))
{
$object = __CLASS__;
self::$instance = new $object;
}
return self::$instance;
}
// others global functions
}
PHP-Code:
namespace account;
use \database;
class class_Account {
// Konstruktor
function __construct() {
// ...
$core = Core::getInstance();
$stmt = $core->dbh->prepare($sql);
$stmt->bindParam(":id",$this->accountId, PDO::PARAM_INT);
$stmt->execute();
}
}
PHP-Code:
> classes
-> account
->-> class_Account.php
-> database
->-> Config.php
->-> Core.php
Fatal error: Class 'account\Core' not found
in der Zeile:
PHP-Code:
$core = Core::getInstance();
Sakron
Kommentar