10

I am writing a PHP extension in C, and I would like to put the classes, functions, and variables I am creating in a namespace. I have not been able to find anything in the extension documentation regarding namespaces. To be clear, I want the equivalent of

namespace MyNamespace{
  class MyClass{
  }
}

but in a C extension. More specifically, I am looking for a function or macro in the Zend C API that allows me to assign a PHP namespace to a class or function I have written in C. Is this possible?

murgatroid99
  • 19,007
  • 10
  • 60
  • 95
  • Yes, that is perfectly fine, I don't normally use it though because I stick to the 1 Class per File. On, never mind I seen your other comment you might want to add the title so it's more visible. – ArtisticPhoenix Jul 10 '14 at 23:03
  • @ArtisiticPhoenix I don't think you understood my question. I know that I *can* do it in PHP. I want to know *how* to do it in a C extension. – murgatroid99 Jul 10 '14 at 23:04
  • @ murgatroid99 I do now, sorry. Is there a tag for C? adding that might help too. – ArtisticPhoenix Jul 10 '14 at 23:05
  • You should brush up on PHP, if any code provided here returns errors at later stages.. You'll only end,back up here.. Which is highly pointless – Daryl Gill Jul 10 '14 at 23:33
  • @DarylGill I don't understand what you are trying to say. I'm looking for a function or macro or something in the PHP C extension API. I have read through the documentation, and it doesn't have anything about this topic. – murgatroid99 Jul 10 '14 at 23:38

2 Answers2

8

Putting a class in a namespace is very simple. Where normally you would initialize a class with

zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "MyClass", my_class_methods);

instead write the second line as

INIT_CLASS_ENTRY(ce, "MyNamespace\\MyClass", my_class_methods);

The namespace does not need to be included in the method declarations or in the members of the my_class_methods array to properly match them with the class.

murgatroid99
  • 19,007
  • 10
  • 60
  • 95
  • Thanks for looping back around to answer the question. I'm sure you've just saved me _a lot_ of time looking for this answer. – ksclarke May 19 '15 at 15:49
  • Following up here, in case anybody is still looking for a succinct answer for both class and function declarations: Since at least 5.3 (when namespaces were introduced) zend_API.h has had the macros `ZEND_NS_FE` and `INIT_NS_CLASS_ENTRY` which are functionally the same as their non-namespaced counterparts with the namespace string right before the function name, e.g. `ZEND_NS_CLASS_ENTRY( ce, "MyNamespace", "MyClass", my_class_methods);` – John P Bloch Feb 05 '21 at 21:15
7

To use Namespaces in PHP extensions, you are basically just putting a prefix in front of the class or function name.

I'm not really a PHP internals developer, so the specifics are not entirely clear to me how this works, unfortunately there is very, very little information online that I could find about this as well (I really put Google through it's paces), and the article below is the best I could find.

However, it seems this article hints at the correct solution, which seems to be, that when you register the function with the Zend engine/PHP internals, you do so like "myNS\\MyFunc" and it should then be accessible from the myNS defined there. I would try out a few different variations with this, and see how far that gets you.

Your best option would be to ask in #php-internals on Freenode (if you can get an invitation) or on the PHP Mailing list.

If you manage to find a solution, the Internet seems to be in need of a good article on how one would accomplish this.

Source http://www.php-cpp.com/documentation/namespaces

A namespace is nothing else than a class or function prefix. If you want your classes or functions to appear in a specific namespace, you simply have to add a prefix to the class or function name....

Update: I've updated my answer to try to be more clear. I'm sorry it took so long, I originally replied from my Phone while I was traveling, with every intention of coming back and responding to your original comment, but I genuinely forgot about it until I got a notification from SO about comments. My apologies.

DavidScherer
  • 863
  • 1
  • 14
  • 26
  • Can you give a description/example of how to do this just using C? I'm not really sure how I would translate the code in that link to the C API. – murgatroid99 Jul 10 '14 at 23:02
  • This does not answer my question at all. It proposes using an extra library in a different language that would apparently require a major rewrite of my extension code to work. And the answer doesn't even explain how to achieve what I want using that library. – murgatroid99 Jul 21 '14 at 21:10
  • This may actually be correct… I think it's saying that you'd just put the namespace and backslash into the name of the function/class/whatever you're defining? Not sure that applies to normal PHP extensions, though. –  Jul 21 '14 at 22:10
  • @murgatroid99 Sorry, I forgot I answered this question, I'd planned on updating after I saw your initial comment after posting, but was on the road and only had my phone and wanted to wait until I had my computer. – DavidScherer Jul 22 '14 at 17:19
  • My self im not an internal php guy, but the way it seem to work is classes with name spaces are just \names\classname so when declaring the class you should be able to declare it's name as such for it to work namespacing is more for ease of use on the user. so `\somenamespace\class` can use by something in the same namespace as just `class` and in others can be short handled by `use \somenamespace\class as someclass` – Barkermn01 Jul 25 '14 at 12:14
  • It turns out that in the case of classes, this is much simpler than I expected. My answer gives the details. Unfortunately, the answer does not apply to functions, so it's not complete. I'll probably give you the bounty because your answer was basically right, even if it didn't explain the specific details. – murgatroid99 Jul 25 '14 at 16:32
  • 1
    i thought all functions were always in the global / default namespace – Barkermn01 Jul 28 '14 at 16:56
  • @MartinBarker I honestly don't know. I haven't actually worked with functions; I just assumed that they could be namespaced like classes. – murgatroid99 Jul 28 '14 at 19:58
  • if you want function's namespacing you might as well just use static functions in a class thats namespaced – Barkermn01 Jul 28 '14 at 20:04
  • 1
    @DavidScherer It is not only perfectly valid to namespace functions, it is even [recommended by the PHP manual](http://php.net/manual/en/userlandnaming.tips.php). – cmb Jun 08 '18 at 15:29