Jump to content


Photo

Calling native libraries from PHP


  • Please log in to reply
9 replies to this topic

#1 anroy

anroy
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 29 April 2006 - 04:08 AM

I need to perform some very large and processing-intensive math calculations. Speed is of the essence here, so I want to make that part run as native compiled object code, written in C/C++. PHP seems to be too slow.

I want to call this C/C++ module from a PHP program running on Apache.

I want to do this for both Linux and Windows.

On the Windows side, I can write a DLL (Dynamically-Linked Library). Can you call a DLL from PHP? If so can you pass parameters to it, and get a return value? (The parameters and return values will, in my case, be lengthy complicated arrays. I guess this complicates things further, right? LOL )

As for the Linux side (where I am an newbie), is there anything akin to DLLs? If so what are they called? Can they be called from PHP at runtime?

Any advice is appreciated, no matter how partial or indirect.

Thanks!


#2 KrisNz

KrisNz
  • Members
  • PipPipPip
  • Advanced Member
  • 271 posts
  • LocationAuckland, New Zealand

Posted 29 April 2006 - 06:07 AM

perhaps [a href=\"http://php.net/com\" target=\"_blank\"]this[/a] is what you're after?

#3 anroy

anroy
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 05 May 2006 - 07:17 AM

[!--quoteo(post=369817:date=Apr 29 2006, 03:07 PM:name=KrisNz)--][div class=\'quotetop\']QUOTE(KrisNz @ Apr 29 2006, 03:07 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
perhaps [a href=\"http://php.net/com\" target=\"_blank\"]this[/a] is what you're after?
[/quote]
Thanks for the reply, but no, I am looking for something more straightforward, like how to call a simple .dll

I don't have the time to learn .NET and COM. Or is that the only option?

I am also looking to do the same thing on a Linux server. So I will need the Windows and Linux solutions to be as similar as possible so I would rather stay at a fairly low level and not abstract things too far from the metal.

I think the libraries on Linux are .so files? I have never programmed on Linux before. But C is common to both platform, and I just want to do math calculations, not any platform-dependent stuff, so I think I should be able to use the same source code, except perhaps for the entry point to the module.

#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 07 May 2006 - 02:18 PM

No need to use .NET at all. COM will let you access dll's as com objects.

Anyway... more to the point. Seems to me, if your going to be doing this in C anyway, your best bet might just be to build an extension for php. You could then either dynamically load the extension using [a href=\"http://php.net/dl\" target=\"_blank\"]dl[/a]() or rebuild php with the extension built in.

#5 neylitalo

neylitalo
  • Staff Alumni
  • Advanced Member
  • 1,854 posts
  • LocationMichigan, USA

Posted 07 May 2006 - 05:09 PM

Since you just want to do math calculations, can't you use the [a href=\"http://www.php.net/manual/en/ref.math.php\" target=\"_blank\"]PHP math library[/a]? Or am I under-estimating the complexity of your calculations? :D

http://nealylitalo.net - My personal website, and home of The Netizen's Journal.

#6 anroy

anroy
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 17 May 2006 - 04:28 PM

[!--quoteo(post=372066:date=May 8 2006, 02:09 AM:name=neylitalo)--][div class=\'quotetop\']QUOTE(neylitalo @ May 8 2006, 02:09 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Since you just want to do math calculations, can't you use the [a href=\"http://www.php.net/manual/en/ref.math.php\" target=\"_blank\"]PHP math library[/a]? Or am I under-estimating the complexity of your calculations? :D
[/quote]
It is running in PHP at the moment but it is slow. As I wrote in my original post I want to speed it up.

Not rocket science in terms of complexity but just a very large number of multiplications and squaring. Looping through hundreds of thousands of these, will possibly increase to millions.


#7 anroy

anroy
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 08 June 2006 - 02:10 AM

Haven't made much progress, this extension is pretty tough [img src=\"style_emoticons/[#EMO_DIR#]/unsure.gif\" style=\"vertical-align:middle\" emoid=\":unsure:\" border=\"0\" alt=\"unsure.gif\" /]

I am looking for some sample code to help me to write the .dll for Windows.

I have found various information on the web on writing PHP extensions but very little that is focused on Windows. Here is one, however it was written a few years ago and uses PHP 4.
[a href=\"http://www.devnewz.com/2002/0909.html\" target=\"_blank\"]http://www.devnewz.com/2002/0909.html[/a]

I would actually prefer if the sample code and, more importantly, the Visual C++ 6 project file itself were available. Project settings are always fraught with difficulties. It is not as if I could not figure it out eventually, the problem is that right now I literally have almost no time. And I would definitely prefer something using PHP 5!

Does anyone know of such a sample anywhere on the web? I realize this may be a long shot but it would help immensely if there is such a thing out there.

(By the way the PHP source code has extension sources in the ext folder, but no Visual C++ project file)

Thanks!


#8 maxim

maxim
  • Members
  • PipPipPip
  • Advanced Member
  • 48 posts

Posted 08 June 2006 - 04:05 PM

maby they didnt make it with visual c++


#9 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 08 June 2006 - 04:34 PM

Yeah... Id'e probably agree. I would think most php developers would be working in Linux. Windows isn't exactly a great developement environment.

#10 anroy

anroy
  • Members
  • PipPip
  • Member
  • 11 posts

Posted 09 June 2006 - 01:43 AM

[!--quoteo(post=381465:date=Jun 9 2006, 01:34 AM:name=thorpe)--][div class=\'quotetop\']QUOTE(thorpe @ Jun 9 2006, 01:34 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Yeah... Id'e probably agree. I would think most php developers would be working in Linux. Windows isn't exactly a great developement environment.
[/quote]
Actually I am going to be developing the exact same thing in Linux soon.

It will be my first time developing anything in Linux (have just started using it). It looks like there will be a huge learning curve but many developers say it is more enjoyable.







0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users