#81 [php] implement %nspace support

Paul Colby

I'm not sure if its even possible to provide a namespace for global functions registered by PHP modules (regardless of whether they were created by SWIG or not).

But, it should still be possible to use the %nspace directive to assign a namespace to the generated *.php wrapper include file. Currently the %nspace directive seems to do nothing at all in PHP mode (is that correct?).

I can get most of the way with something like: %pragma(php) code="namespace qpid\messaging;"

But, PHP requires namespace statements to be the very first non-comment statement in any given PHP file, but the %pragma(php) code ends up being immediately after the "Try to load our extension if it's not already loaded" code block, causing the include of the wrapper *.php class file to fail.

It seems to me that "%nspace mynamespace" should result in "namespace mynamespace;" being added at the very beginning of the generated *.php class wrapper.

Also, if would be great if the %nspace directive would automatically translate :: and \ separators. For example, "%nspace ns1::n2::n3" should map to "ns1::ns2::ns3" for C#, and "ns1\ns2\ns3" for PHP, and vice versa. That would be great! :) Otherwise, we'd need different %nspace directives for different destination languages.


  • William Fulton

    William Fulton - 2011-02-01

    %nspace has only been implemented for Java and C# at as of swig-2.0.1. Supporting this will require quite a lot of target language specific knowledge and implementation (I don't know of any current plans to add this in).

  • Paul Colby

    Paul Colby - 2011-02-01

    I suspected that would be the case... perhaps this artifact should be changed to a feature request then? (Can artifacts be moved?)

    Thanks :)

  • Olly Betts

    Olly Betts - 2011-02-03
    • summary: %nspace does not work for PHP --> [php%nspace does not work for PHP
    • labels: 379158 -->
  • Olly Betts

    Olly Betts - 2011-02-03


    Another PHP-specific issue is that namespaces in the language itself are relatively new (in PHP5.3 IIRC). Not sure if there's much we can do about that, other that point out that you shouldn't use it unless you're happy to require PHP >= 5.3.

  • Olly Betts

    Olly Betts - 2011-02-03
    • summary: [php%nspace does not work for PHP --> [php] implement %nspace support
  • Paul Colby

    Paul Colby - 2011-02-06

    Ah, that's a good point. I am using a very simple sed command as a workaround, so given the fairly-high PHP >= 5.3 requirement, it probably makes sense for my post-SWIG make system to apply the sed or not, depending on the installed version of PHP.

    Of course, it would be great if SWIG could apply the %nspace directive or not, depending on the installed PHP version, but I assume that SWIG currently has no visibility of the installed PHP version (indeed, I assume that SWIG can generate PHP wrapper *.cpp / *.php files without even having PHP installed), and that would then require flags to set a target PHP version for cross-version development etc. That all sounds like a lot more effort than simply using a single post-SWIG sed command.

    For anyone that's interested, the sed command I'm using is:

    sed -i -e 's/^<?php/<?php namespace my\\project\\namespace;/' [module_name].php

    Easy enough :)

  • Olly Betts

    Olly Betts - 2012-12-17

    PHP 5.2 was EOLed by the PHP developers on 2011-01-06 (and the final 5.2 release was made that day). Older PHP releases don't die as quickly as the PHP devs like to think, but compatibility with releases without namespace support is heading for being a non-issue.

    Certainly an option to support %nspace in PHP would be useful to people who no longer care about PHP < 5.3.

  • Olly Betts

    Olly Betts - 2017-01-20
    • status: open --> closed
    • Group: -->

Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks