Can I put PHP extension classes, functions, etc. in a namespace?
Asked Answered
E

2

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?

Eckstein answered 10/7, 2014 at 22:43 Comment(5)
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.Riven
@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.Eckstein
@ murgatroid99 I do now, sorry. Is there a tag for C? adding that might help too.Riven
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 pointlessTessellation
@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.Eckstein
E
9

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.

Eckstein answered 25/7, 2014 at 16:31 Comment(2)
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.Confide
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);Shiksa
E
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.

Esperanzaespial answered 10/7, 2014 at 22:58 Comment(10)
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.Eckstein
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.Eckstein
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.Premonitory
@Eckstein 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.Esperanzaespial
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 someclassVal
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.Eckstein
i thought all functions were always in the global / default namespaceVal
@MartinBarker I honestly don't know. I haven't actually worked with functions; I just assumed that they could be namespaced like classes.Eckstein
if you want function's namespacing you might as well just use static functions in a class thats namespacedVal
@Esperanzaespial It is not only perfectly valid to namespace functions, it is even recommended by the PHP manual.Bombay

© 2022 - 2024 — McMap. All rights reserved.