How to import an extension such as pgsql into namespace?

Well right now I am trying to design a database system with adapters such that you can use generic database objects such as Database(connection), Statement, PreparedStatement, ResultSet and RowSet with same method signatures no matter whether you use MySQLi, PDO, PgSQL or SQLite etc. The problem I am having right now is that, well, since my classes are namespaced when I use pg sql functions I get errors like this:


 Call to undefined function Mysidia\\Resource\\Database\\Adapter\\pg_connect()  in /home/mysidia/public_html/site/framework/database/adapter/postgresqladapter.php on line 49

So here are my questions. First of all, I thought global functions are automatically imported or that at least its name resolution should start at the root global namespace. Did PHP change this behavior in version 5.4 or that was I mistaken in the very first place?

Second, is there a way to import functions by importing extensions with prefixes into the current namespace? I can get away with by adding a leading backslash to go back to the global namespace, but I hate using leading backslashes as they are one of the ugliest syntax I’ve ever seen, why does PHP’s namespace resolution work so differently from other languages? sigh

At last, even if I end up using the ugly leading backslash like \pg_connect and \pg_query, is this trick even gonna work with earlier versions like PHP 5.3 as my software’s lowest requirement is PHP 5.3(though I run my own PHP 5.4 server, it does become somewhat complex to compensate for PHP 5.3 users, but hey its still way better than having to worry about compatibility with outdated PHP 5.2 lol).

Thx.

It should work, yes. It seems in this instance that PHP can’t find that function anywhere and reports that the function doesn’t exist in the namespace, when in fact it doesn’t exist at all because the extension isn’t installed. I vaguely recall a similar instance I had a few months back.
Can you please check that all required PHP extensions are installed and loaded (via [fphp]phpinfo[/fphp])?

I see, and yeah it does seem to me that pgsql was not installed. I’ve now installed this extension and it seems to be working, thanks for your advice. Still, the fact that PHP class name resolution starts at current namespace is a mis-feature, cant think of any other programming languages doing this and the leading backslash like \ArrayObject and \Exception gives me a headache all the time, just so ugly…

I couldn’t agree more! :slight_smile: