Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 782
murgatroid99 Avatar asked Jul 10 '14 22:07

murgatroid99


People also ask

Which types are supported in PHP namespaces?

A namespace can contain valid PHP code. Namespace affects following types of code: classes (including abstracts and traits), interfaces, functions, and constants.

What is the best approach for working with classes and namespace in PHP?

To address this problem, namespaces were introduced in PHP as of PHP 5.3. The best way to understand namespaces is by analogy to the directory structure concept in a filesystem. The directory which is used to group related files serves the purpose of a namespace.

Which of the following elements are affected by namespaces in PHP?

only four types of code are affected by namespaces: classes, interfaces, functions and constants.

What is the difference between namespace and use in PHP?

A namespace is a way of grouping identifiers so that they don't clash. Using a class implies that you can create an instance of that class, not true with namespaces. 2. You can use using-declarations with namespaces, and that's not possible with classes unless you derive from them.


2 Answers

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.

like image 131
murgatroid99 Avatar answered Oct 14 '22 01:10

murgatroid99


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.

like image 7
DavidScherer Avatar answered Oct 14 '22 00:10

DavidScherer