Page 1 of 1

Global Classes not working (Xampp 6.8.0, Opencart 2.0)

PostPosted: 22. June 2015 12:42
by JasperB
Global classes seem not to work in Xampp, im calling the PDO class whitin a new namespace but it cannot call the class

Fatal error: Class 'DB\PDO' not found in C:\xampp\vhost\oc2.jasper.lan\httpdocs\system\library\db\mpdo.php on line 9

the class looks as follows
Code: Select all
namespace DB;
final class mPDO {
   private $pdo = null;
   private $statement = null;

   public function __construct($hostname, $username, $password, $database, $port = "3306") {
      try {
         $this->pdo = new \PDO("mysql:host=" . $hostname . ";port=" . $port . ";dbname=" . $database, $username, $password, array(PDO::ATTR_PERSISTENT => true));
...

Link to the full class https://github.com/opencart/opencart/blob/master/upload/system/library/db/mpdo.php

the following snipit shows that the Global namespace is being adressed rather then the current namespace DB
Code: Select all
$this->pdo = new \PDO...


Does anyone know a fix for this ?, I'm thinking this is a misconfig in php.ini

Re: Global Classes not working (Xampp 6.8.0, Opencart 2.0)

PostPosted: 22. June 2015 13:44
by JasperB
Ok i found a workaround for now by adding "use \PDO;" just after the namespace

Re: Global Classes not working (Xampp 6.8.0, Opencart 2.0)

PostPosted: 22. June 2015 15:32
by mark.mcdonald
This is not a xampp issue. C:\xampp is the default installation destination but everything after that vhost\oc2.jasper\.. is not native to (is not downloaded with) xampp.
OpenCart does not come with xampp but has been built to use on top of xampp. I would re-post this to danielkerr (poster of https://github.com/opencart/opencart) and address this issue to him.

Glad you found a workaround in the meantime and wish you the best of luck.

Re: Global Classes not working (Xampp 6.8.0, Opencart 2.0)

PostPosted: 22. June 2015 15:49
by Altrea
Hi Jasper,

The Problem with your code is, that you didn't prefixed every occurance of PDO with the global namespace.
You forgot the attribute PDO::ATTR_PERSISTENT which need to be \PDO::ATTR_PERSISTENT if you want to use it inside a namespace.

use \PDO; will also work, but is not really needed.

best wishes,
Altrea