PHP and C++

PHP interface with a C++ back end.

Page 1 of 1

12 Replies - 5872 Views - Last Post: 07 January 2011 - 04:22 AM Rate Topic: -----

#1 sas1ni69  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 431
  • Joined: 04-December 08

PHP and C++

Posted 05 January 2011 - 08:04 PM

Hi guys,

I have come across articles where they say that some web apps are developed with C++ and PHP. How does that work? I can't seem to find any good read on the subject.

Thanks in advance.

Sas1ni69

P/s: I wasn't sure whether to post this in the PHP or C++ forums.
Is This A Good Question/Topic? 0
  • +

Replies To: PHP and C++

#2 stevo86  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 33
  • Joined: 05-January 11

Re: PHP and C++

Posted 05 January 2011 - 09:57 PM

You can use C++ to create extensions for PHP. Basically you're creating C++ functions that can then be accessed through PHP. Here's a few introductory articles on the subject:
http://devzone.zend.com/article/4486
http://devzone.zend....to-PHP-and-Zend
Was This Post Helpful? 1
  • +
  • -

#3 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 925
  • View blog
  • Posts: 3,204
  • Joined: 19-January 10

Re: PHP and C++

Posted 06 January 2011 - 02:29 AM

What would be the point? What could I do in c++ that I can't in PHP?
Was This Post Helpful? 1
  • +
  • -

#4 stevo86  Icon User is offline

  • New D.I.C Head

Reputation: 13
  • View blog
  • Posts: 33
  • Joined: 05-January 11

Re: PHP and C++

Posted 06 January 2011 - 02:41 AM

View Postcreativecoding, on 06 January 2011 - 08:29 AM, said:

What would be the point? What could I do in c++ that I can't in PHP?


I can't think of much but just about every piece of built-in functionality is written in C++, from cURL to strings to variables. I guess you could create new types or classes with and build in support for them. I imagine it's more of a long-term corporate strategy type of thing, not really for individuals making a website here and there.

I really can't think of too many situations at this point that would merit using C++ to write an extension but it is just about 5AM here and I'm running off all of 2 hours of sleep for past ~50 hours or so. Maybe I just can't think straight enough to give you a good answer right now. Who knows. But that's all I know about C++ and PHP being used together for web applications.
Was This Post Helpful? 1
  • +
  • -

#5 sas1ni69  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 85
  • View blog
  • Posts: 431
  • Joined: 04-December 08

Re: PHP and C++

Posted 06 January 2011 - 02:57 AM

That's really great. Thanks a lot Steve. As to why they'd choose C++ to write the extensions instead of using PHP directly, I'm not quite sure, but if I had to take a guess I'd say it'll improve the performance for really high trafficked sites.

Maybe? I really don't know. Thanks again.
Was This Post Helpful? 0
  • +
  • -

#6 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3470
  • View blog
  • Posts: 9,972
  • Joined: 08-June 10

Re: PHP and C++

Posted 06 January 2011 - 04:41 AM

indeed. C++ code is already compiled while PHP code has to be parsed every time. besides you can’t use standard libraries (like mysqlnd) with PHP.

This post has been edited by Dormilich: 06 January 2011 - 04:42 AM

Was This Post Helpful? 1
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6021
  • View blog
  • Posts: 23,395
  • Joined: 23-August 08

Re: PHP and C++

Posted 06 January 2011 - 05:07 AM

One of the other ways in which a web app is written in C++ and PHP is through the use of SOAP. The C++ routines are exposed to PHP through a contract called WSDL, which can then call the C++ routines directly and get data back. I suspect this is the more likely meaning here; I don't think there are a lot of people developing PHP extensions for interfacing with C/C++ code (even though that was really my first exposure to PHP, but that was like 12 years ago and I'm pretty sure that sort of pre-dated the SOAP, at least in wide support).
Was This Post Helpful? 2
  • +
  • -

#8 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: PHP and C++

Posted 06 January 2011 - 12:13 PM

View PostDormilich, on 06 January 2011 - 05:41 AM, said:

...while PHP code has to be parsed every time.

That's only true if you're not using an opcode cache. A caching extension like XCache or APC will store a script's compiled opcode in memory and execute it directly, eliminating the time required to load and parse the file. They can easily double or triple performance just by flipping a switch, which is why everyone who counts uses one.

Of course, even if that factor eliminated, you can probably get better performance out of C++ than PHP for computation-heavy jobs, so there's still some benefit there. And as you pointed out, binding to additional libraries is another good reason to write an extension. For example, a buddy who works for a company that builds mapping software was just telling me that they use in-house PHP extensions to interface with some C++ libraries manipulating geospatial data.

View PostJackOfAllTrades, on 06 January 2011 - 06:07 AM, said:

One of the other ways in which a web app is written in C++ and PHP is through the use of SOAP.... I suspect this is the more likely meaning here...

That seems like a bit of a stretch. There's absolutely nothing about SOAP that's directly linked to either PHP or C++. To say that an app specifically described as "developed with C++ and PHP" is probably using SOAP is jumping to conclusions. Writing PHP extensions may not be an every-day occurrence, but it's not that uncommon. It's not like there aren't plenty of extensions out there.
Was This Post Helpful? 1
  • +
  • -

#9 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6021
  • View blog
  • Posts: 23,395
  • Joined: 23-August 08

Re: PHP and C++

Posted 06 January 2011 - 12:44 PM

You think there are more companies who, when implementing a web application, are writing custom PHP extensions than hooking their PHP system to a web service for a web app?

My experiences in the interface between front-end and database have been:

1a. A home-brewed CGI-type of system (pre-dating PHP)
1b. Python 1.5.2 direct to DB and filesystem
2. PHP 3/4 with custom extensions (no true DB here)
3. PHP 4/5 direct to DB and filesystem
4. PHP 5 through gSOAP
5. Ruby on Rails

Obviously I've seen a variety. Given the open-ended nature of the question, I don't think my answer is all that bad. While I agree SOAP isn't the only way to do this, it is a widely-used web standard. And although I can't be sure given my limited exposure to different systems (I am not a job-hopper), I doubt that C++ and ancient CGI technology is all that popular as a front-end.

Listen, I'm no zealot for anything. I was just offering another option/opinion based on my experiences, so sorry if I came off as authoritative.
Was This Post Helpful? 1
  • +
  • -

#10 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: PHP and C++

Posted 06 January 2011 - 04:19 PM

View PostJackOfAllTrades, on 06 January 2011 - 01:44 PM, said:

You think there are more companies who, when implementing a web application, are writing custom PHP extensions than hooking their PHP system to a web service for a web app?

Of course not. But I wasn't questioning the validity of the solution - I was questioning the logic of making that connection.

My objection was simply to the characterization of SOAP usage as "the more likely meaning" of "developed in C++ and PHP". Granted, the original question was very open-ended, but nothing about it suggested that web services were involved at all, let alone SOAP-based ones. Sure, "developed in C++ and PHP" could mean that, but it could also mean a PHP extension or interacting with COM objects. Heck, it could theoretically mean talking to a local daemon over a UNIX domain socket or even just shelling out to a C++ console program. My point is that, given the sparse level of detail, calling the SOAP scenario the "likely" intention is really reaching.

Not that you don't raise a good point. Web services are a great tool, especially when it comes to integrating disparate technologies. They're also becoming increasingly common and, in most cases, are to be preferred to trying to bridge systems using native libraries. I just don't think there's much chance that that's what the OP was reading about.

Quote

I doubt that C++ and ancient CGI technology is all that popular as a front-end.

Heh.... Funny you should mention that, because that's exactly what our parent company does. They sell a web-based collaboration system (e-mail, calendaring, IM, etc.) which is all C++ and FastCGI on the server side. Don't ask me why - I think it's completely insane. Hopefully they're just the exception that proves the rule.
Was This Post Helpful? 1
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6021
  • View blog
  • Posts: 23,395
  • Joined: 23-August 08

Re: PHP and C++

Posted 06 January 2011 - 04:28 PM

It's not Onebox, is it?
Was This Post Helpful? 0
  • +
  • -

#12 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: PHP and C++

Posted 06 January 2011 - 07:58 PM

View PostJackOfAllTrades, on 06 January 2011 - 05:28 PM, said:

It's not Onebox, is it?

No, different market. They don't do voice - think GMail + Google Calendar + IM (text-only) + file sharing. Except buggy, unintuitive, and not free. They were positioning themselves as an alternative to MS Exchange for small businesses...until they started reselling hosted Exchange. I have no idea what the business plan is now, but since I work for a spin-off, I'm not in the loop.
Was This Post Helpful? 0
  • +
  • -

#13 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6021
  • View blog
  • Posts: 23,395
  • Joined: 23-August 08

Re: PHP and C++

Posted 07 January 2011 - 04:22 AM

Strictly from the Exchange side it sounds like a somewhat similar area as my former company. Hosted Exchange :shudder:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1