8

Python has ctypes to access libraries. In PHP you write extensions for everything. Popular extensions like the one for libgd are available almost everywhere.

Is there any extension which works like Python's ctypes, letting you access libraries without the need to write an PHP extension?

stesch
  • 7,202
  • 6
  • 47
  • 62

5 Answers5

2

You're looking for ffi.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

PHP 7.3 will have FFI (Foreign Function Interface).

stesch
  • 7,202
  • 6
  • 47
  • 62
  • More info: https://www.php.net/manual/en/book.ffi.php https://www.php.net/manual/en/ffi.examples-basic.php Note that as of PHP 7.3 it is still marked experimental. – pcworld May 24 '19 at 22:23
  • A more accurate statement is that starting with PHP 7.3, FFI is available for general use. For those looking at the PECL extension and thinking, "This looks outdated." That would be because the FFI PECL extension was merged into the core product as its own extension. Since it's included with PHP itself (e.g. via package managers like Debian apt), there's no reason to separately maintain the PECL extension anymore. I highly recommend limiting access to FFI to just the PHP CLI because it can be used to easily segfault PHP. – CubicleSoft Jan 23 '21 at 22:15
1

There is a PHP extension (irony?) called ffi. FFI stands for Foreign Function Interface, which is the generic term for when a language calls libraries written in another language.

Anthony
  • 36,459
  • 25
  • 97
  • 163
0

I don't think there is such a thing : in PHP, the "standard" way of using a library is by writting a wrapper arround it, that exports the functions of the library to PHP.

(But maybe an extension could be written to do just what ctypes does ? -- Not sure, but maybe ^^ )

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • It's definately possible, though I see some pretty huge security issues here. Beginner PHP programmers aren't known to be able to control all aspects of their app (and neither does half of the experienced ones). What if someone manages to run some injected code that uses ctypes in PHP -- that includes being able to load external libraries, otherwise what's the point. All of PHP's security model (if there ever was such a thing) would be compromised. – Tor Valamo Jan 28 '10 at 06:02
0

I don't know of any way. But you could let SWIG build an extension for the library you want.

user86297
  • 162
  • 1
  • 4
  • 1
    Not exactly what I had in mind. But at least I don't have to write the extension on my own. – stesch Jan 29 '10 at 22:22