简体   繁体   中英

How would I go about expanding my PHP code library with Python code?

I have a rather large API written in PHP that I've worked on for years. Some of the functionality needs to be upgraded to the extent where I should really just rewrite the classes. Since I need to scrap the old code anyway, I was thinking perhaps I could replace the old functionality with Python code.

I came across PiP while searching for answers, and it seems like an exellent solution (Since I can actually create Python class instances in PHP and call their methods etc.) but it seems it was abandoned in the Alpha stages for reasons unknown to me.

I suppose the simplest solution would be a CLI one, meaning I could run a python instance from PHP and collect the results. I don't particularly like this solution though, considering the amount of Python code I'd have to write just to handle input from PHP and respond accordingly. Plus it's not very reusable.

I don't know if this is a normal problem, Google certainly don't seem to think so, but what would be the best way of complementing a PHP code library with Python code?

I can think of two options, which should also make the overall solution somewhat more flexible:

  • Write it as a web service: Write the python parts as a RESTful web service. Should be relatively straightforward to do.
  • Use something like ZeroMQ to create a message queue: There are zmq libraries for both PHP and Python, which should also make this option not very difficult to implement.

As you may have noticed, all the options can be somewhat clunky in nature. You didn't mention what exactly did you mean by "API" (eg. is it a library or web service or what), so depending on what it is, it might just be easiest to rewrite it in PHP - it's not a bad language in the end if used properly.

After some time researching this, I've concluded that there is no worthwhile way of building on a PHP library with Python code. The PiP project was the closest thing I could find but it has been abandoned.

My solution has been to declare a 2.0. Every new function I add I write in Python. I translate some code from PHP to Python when necessary. It's working great so far!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM