Global Classes not working (Xampp 6.8.0, Opencart 2.0)

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Global Classes not working (Xampp 6.8.0, Opencart 2.0)

Postby JasperB » 22. June 2015 12:42

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
JasperB
 
Posts: 2
Joined: 22. June 2015 12:30
Operating System: Win 8.1

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

Postby JasperB » 22. June 2015 13:44

Ok i found a workaround for now by adding "use \PDO;" just after the namespace
JasperB
 
Posts: 2
Joined: 22. June 2015 12:30
Operating System: Win 8.1

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

Postby mark.mcdonald » 22. June 2015 15:32

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.
mark.mcdonald
 
Posts: 160
Joined: 13. March 2015 15:48
Location: Edmonton
Operating System: Windows Server 2012 R2

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

Postby Altrea » 22. June 2015 15:49

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
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 11926
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 11 Pro x64


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 78 guests