7

How can I call PHP functions from my C application?

Example:

include <php.h>

int main()
{
  return json_encode(""); // This is a PHP function coming from php.h
}

Note: PHP function collections is very managed and organized, i just want to have that logic in my C application, everything organized on-demand. Thats the beauty i found in PHP language.

  • 6
    AFAIK this won't be possible or really difficult and lead to many problems. And since the major part of PHP is written in C, there's nothing you can't do in C with a library or language functionnality. Why do you want to do that ? Why not use PHP directly ? – krtek May 26 '11 at 20:24
  • 3
    Without some crazy bit of hacking you shouldn't be able - PHP extension functions are defined in a very different way than in C. To use them you would need to push in zvals into the PHP functions, provide them with a PHPy-enough enviroment, a.s.o. – NikiC May 26 '11 at 20:24
  • 1
    If you add an explanation of _why_ you want to do this (that is, what you're really trying to do), you'll get a much nicer set of answers. If the "why" is because you're used to php and need to write some C code, the answer is to learn C, or just use PHP. – jdd May 26 '11 at 20:38
  • There's a book called Extending and Embedding PHP that apparently deals with this. However, it was written in 2006 and could be out of date. Here's a [Slashdot review](http://books.slashdot.org/story/06/07/31/1416207/Extending-and-Embedding-PHP) and an [Amazon page](http://www.amazon.com/Extending-Embedding-PHP-Sara-Golemon/dp/067232704X) that may help. – CanSpice May 26 '11 at 20:45
  • You may also find [this blog post](http://phi.lv/?p=376) informative. – CanSpice May 26 '11 at 20:58
  • 6
    @jeremiahd: PHP function have a managed programming concept/standards like quick push to move fast and fury. In C language a common pattern does not exist (string/socket/etc etc not managed), everything requires to build from day 1. So if i can import PHP standard functions in pure C code i will use PHP functions only, without PHP interpreter involved. –  May 26 '11 at 21:22
  • 2
    all my life i ran implementing c functions in php, n u jst surprised me by asking this question, i m really interested in knowing your app demands that forces u to call php from inside of c – Abhinav Singh May 26 '11 at 21:40
  • 6
    @Abhinav Singh: Its very organized language not only the shape but the beauty of functions collection, such as http://phpjs.org is doing using js. I am willing to have header file which can allow me to do it with pure C without involving PHP interpreter. –  May 26 '11 at 21:46
  • 5
    PHP is **not** "very managed and organized". There are **no** naming conventions nor parameter order conventions or any conventions of any sort. The PHP standard function library is an ugly mess. – Matti Virkkunen May 26 '11 at 22:06
  • 1
    I wish comments had downvote buttons – Matti Virkkunen May 26 '11 at 22:15
  • @Matti I agree with the statement, that PHP is a mess. With time it is becoming more and more organized I think, though. Unfortunately, it does not seem they will be fixing mistakes from the past... – Tadeck Jun 30 '11 at 23:45

4 Answers4

10

Since I also want my downvote today, I will try to answer to this question ;)

I don't know if what you're asking is possible. But I'm sure of some other things :

  1. The result will be really complicated, slow and will lead to many problems with dependencies and portability.
  2. You will get much better answer if you try to explain why you're asking this and what are the goals you want to achieve
  3. The major part of PHP is implemented in C, so everything your doing in PHP can be done in C too. Concerning this, here's some reading about Turing Completeness.
  4. Learning the C language instead of wanting to use some known PHP functions will open you new perspectives which will only improve your coding skills.
  5. You should ask you if you really want / have to use C, or if it's better to use some other higher level language.
  6. If you want to decode JSON messages in C, http://www.json.org/ lists plenty of C implementation of the standard you can use. For base64 decoding, see this How do I base64 encode (decode) in C? for example.
Community
  • 1
  • 1
krtek
  • 26,334
  • 5
  • 56
  • 84
  • @Marie I have only one answer for you : use PHP and not C ;) And BTW, like I already said, phpjs is a reimplementation in Javascript, it's a totally different thing. – krtek May 26 '11 at 21:45
  • 1
    @Marie: **None of the characters you have typed anywhere in this thread are making any sense** – Matti Virkkunen May 26 '11 at 22:29
  • @Matti Virkkunen: This answer makes no sense at all, i wish there was a downvote of 50K at once. I think the the writer dont speak English or cant read. The question was asked having the functions of PHP to C as a collection, on-demand use it. –  Jun 04 '11 at 08:15
3

No you cannot. You can however "extend" PHP using C and might be able to cook something up like this but it's not really useful. Why do you want to do this?

Noufal Ibrahim
  • 71,383
  • 13
  • 135
  • 169
  • 1
    @Marie php.js is a reimplementation of some php functions in Javascript, this has nothing to do with what you're asking. – krtek May 26 '11 at 20:54
  • @Marie there's many C library which deals with JSON and base64 related stuff, have a look at my answer for some pointer. There is also plenty of function to work with sockets. Function like isset() don't make a lot of sense in C. You should ask yourself if C is the right language for what you're trying to achieve and, if it's the case, try to really learn it insteand of trying to use PHP functions. – krtek May 26 '11 at 21:18
  • @Marie then why are you doing C and not PHP ? There's also plenty of other languages you can use : Python, Java, etc – krtek May 26 '11 at 21:27
  • 1
    phpjs is a reimplementation of a subset of PHP in javascript - it's not like what you're asking here. "Managed" functions doesn't sound like a valid argument to me. The call semantics and general organisation is different for different languages. If that's a deal breaker for you, you should consider moving to the other language. If you want to write command line programs in PHP, you can use PHP as a regular interpreter outside your web server. The bottom line. Why do you want to do this? To CanSpice: Can you give me some pointers to where people have done (and used) this? – Noufal Ibrahim May 27 '11 at 05:50
1

Since C is faster than PHP, and PHP is written in C, I am thinking that trying to use PHP functions in C would make your program unnecessarily slow. And I am pretty sure that it is impossible.

  • 1
    Right, so if you are trying to use php functions in a C program, and those functions have to go through an interpreter, they would be slower. Why the heck did i get voted down? Isn't this impossible? –  May 26 '11 at 20:44
  • 2
    @Corey it's not me :) I think 'pure' C-functions of PHP shouldn't go through the interpretor, only php-scripts should. But it's only guesses. – OZ_ May 26 '11 at 20:45
  • -1 because it's not impossible, people have embedded PHP in C in the past. – CanSpice May 26 '11 at 20:46
  • 1
    @CanSpice instead of downvoting everyone, provide some sources and exemple ;) And he said that he's *pretty* sure... BTW, the feasibility of something don't imply that it is a good idea. – krtek May 26 '11 at 20:55
  • @CanSpice, absolutely, ANSWER the question then, or maybe commenting is your way of answering without anyone being able to "vote" on your answer? –  May 26 '11 at 20:57
  • @Krtek: I don't know how to embed PHP in C, which is why I haven't answered. I do know that it's possible, and therefore the answers saying it's not possible are wrong, and downvoting wrong answers is kind of what downvoting is about. – CanSpice May 26 '11 at 20:57
  • @CanSpice, I'd say unless you can show us some code that does what the OP is asking, you're wrong in downvoting the answers that say it can't or shouldn't be done. –  May 26 '11 at 20:58
  • @CanSpice this answer isn't wrong, Corey said *pretty* sure ;) In a way, everything is possible in computer science, the question is just if you want to spend the time doing it and if you have something to gain. In this precise case, I really doubt there's anything positive in trying to call PHP functions in C. – krtek May 26 '11 at 21:00
0

You could, and actually this is a project I have in mind.
What has to be done is to get the PHP SPL code and just rewrite it (clean it a little) to be used as a regular C library.
I'd like to do it some day.

Petruza
  • 11,744
  • 25
  • 84
  • 136