开发者

Is it possible to call PHP's C functions in a C program?

开发者 https://www.devze.com 2023-02-02 09:19 出处:网络
When you download PHP\'s source you realize that all the goodies are in the ext/ directory in their respective array.c, string.c, etc. files.

When you download PHP's source you realize that all the goodies are in the ext/ directory in their respective array.c, string.c, etc. files.

Now, I was wonder开发者_开发问答ing, it is possible to call these functions from a C program having those PHP C files?


You can do it but that doesn't mean it's a good idea. The PHP library functions deal with PHP's internal data types, memory allocation and management schemes, etc. You'd probably end up wasting a lot of time and effort on marshalling data between PHP and C.

Most of the functionality is probably available in C libraries or easily implemented in C (with C style interfaces) anyway. Also, I suspect that you'd end up linking the entirety of PHP into your program just by accessing a couple of PHP's functions.

Certainly possible but probably more trouble than it's worth. If you're writing in C then write in C, some unnatural bastardized mix of C and PHP would just get you a lot of funny looks.


It is possible in theory, the question is why do you want to do it. Note that PHP functions deal with the data stored in PHP data structures and frequently require various PHP structures and settings to be initialized in order to work.

So, if you want to add your C code to PHP, look at writing PHP extensions.

If you want to have PHP engine inside your C program, this is possible with embedding, but it's very rarely done as for most tasks it's an overkill.

If you just want to reuse some code, PHP license is like BSD, so you can just take the code in most cases, provided you follow the license, but be careful about these external dependencies I mentioned above.


Well, why not? Just resolve any dependencies and off you go...

Of course there's licensing worth checking out. But code wise, it should work.

0

精彩评论

暂无评论...
验证码 换一张
取 消