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.

11
  • 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 ? Commented May 26, 2011 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. Commented May 26, 2011 at 20:24
  • 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. Commented May 26, 2011 at 21:22
  • 6
    @Abhinav Singh: Its very organized language not only the shape but the beauty of functions collection, such as 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. Commented May 26, 2011 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. Commented May 26, 2011 at 22:06

4 Answers 4

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.
Sign up to request clarification or add additional context in comments.

3 Comments

@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.
@Marie: None of the characters you have typed anywhere in this thread are making any sense
@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.
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?

4 Comments

@Marie php.js is a reimplementation of some php functions in Javascript, this has nothing to do with what you're asking.
@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.
@Marie then why are you doing C and not PHP ? There's also plenty of other languages you can use : Python, Java, etc
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?
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.

8 Comments

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?
@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.
-1 because it's not impossible, people have embedded PHP in C in the past.
@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.
@CanSpice, absolutely, ANSWER the question then, or maybe commenting is your way of answering without anyone being able to "vote" on your answer?
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.