Questions tagged [php-extension]

The PHP language can be enhanced through the use of extensions, which provide to PHP scripts new internal functions, classes, resource types, streams (also stream wrappers and filters) and, in general, can do anything a native program would be able to do.

The PHP language can be enhanced through the use of extensions, which provide to PHP scripts new internal functions, classes, resource types, streams (also stream wrappers and filters) and, in general, can do anything a native program would be able to do. Extensions are typically written in order to:

  • Wrap a native library, which is typically written in C or C++.
  • Implement PHP code in a language that offers more optimization opportunities, such as C, so that pages can be served faster and/or more pages can be served concurrently.
  • Implement/functions classes that, being closer to PHP's core, are able to do things that would otherwise not be possible (e.g. add/remove functions from the function table dynamically, bypass open_basedir).

PHP is written in C and has a C API available for extensions, therefore these tend to be written in that language. However, the only requirement is that the resulting extension is able to interface with PHP's APIs, handle its data structures and can be loaded by PHP's extension loading mechanism.

SWIG can be used to wrap native libraries in an easier fashion. See also PHP4Delphi, which allows embedding PHP in Delphi and other applications as well as writing PHP extensions in Delphi.

A special type of extensions, Zend extensions, are more tightly coupled with PHP and are provided with more hooks so that they can modify PHP's behavior more extensively. They are typically used to implement debuggers (e.g. XDebug).

Unfortunately, there is no official and up-to-date reference on how to write PHP extensions. However, see this answer for several resources.

1016 questions
8
votes
1 answer

Passing a variable by reference into a PHP extension

I'm writing a PHP extension that takes a reference to a value and alters it. Example PHP: $someVal = "input value"; TestPassRef($someVal); // value now changed What's the right approach?
John Carter
  • 53,924
  • 26
  • 111
  • 144
8
votes
3 answers

How to enable PHP Intl extension on macOS Mojave?

I'm trying to install Magento (2.3.0) on macOS Mojave. Magento shows PHP Extension intl. is missing. I tried the below to resolve: Made a copy of php.ini using cp /etc/php.ini.default php.ini Removed ";" before extension=php_intl.dll Restart…
Suren Konathala
  • 3,497
  • 5
  • 43
  • 73
8
votes
3 answers

How to add php gd extension to Dockerfile

I'm getting this error when trying to deploy using alpine: phpoffice/phpspreadsheet 1.2.1 requires ext-gd * -> the requested PHP extension gd is missing from your system. Here's my Dockerfile: FROM php:7.2-alpine RUN apk update RUN apk add…
mikelovelyuk
  • 4,042
  • 9
  • 49
  • 95
8
votes
3 answers

PHP extension vs. library (and can it be converted)

Some php wamp/lamp packages come with php extensions packaged within like php_amf, php_db, php_gd2 and I just have to activate the extension, or install the extension if it doesn't come by default. My question in general is, how are these…
sami
  • 7,515
  • 8
  • 33
  • 37
8
votes
3 answers

How do I compile a PHP extension with Microsoft Visual C++ 2008?

I created a PHP Hello World extension DLL with Microsoft Visual C++ 2008. I have the right php.ini (I know because when I enable and disable the gd2 extension, there is an effect), but when I load the extension it does not pop up under the…
Mark Lalor
  • 7,820
  • 18
  • 67
  • 106
8
votes
3 answers

PHP 5.6: headers_sent intermittently returns true, empty file name, and line 0

I'm intermittently getting this issue in my PHP scripts (PHP 5.6, Apache 2.2): Warning: Cannot modify header information - headers already sent in /path/to/index.php on line 55 This warning doesn't have the "sent by" portion that I've seen in…
Aaron
  • 2,049
  • 4
  • 28
  • 35
8
votes
4 answers

Install PHP intl extension on MacOS

I am facing some issues in the installation of Cakephp. I hope any one had faced the same issue. The error - cakephp/cakephp 3.3.9 requires ext-intl * -> the requested PHP extension intl is missing from your system. As per the error message, I…
Nishant Sethi
  • 97
  • 1
  • 1
  • 3
8
votes
2 answers

What is the difference between Mysqlnd, PDO and PDO_Mysql extensions in php?

They seem to be the same thing, and yet different. I don't know which one is which: I know we can use PDO as new PDO() and use prepare() and query() methods, to fetch data from database. So, if this is PDO mentioned in the extensions list, then what…
ʎɹnɔɹǝW
  • 811
  • 2
  • 8
  • 14
8
votes
1 answer

PHP Extension using libtidy compiles, but does not load

I wrote an extension in C++ that uses libtidy, and it runs perfectly under PHP when I compile PHP --with-tidy. However, it would be nice to have the extension run on a vanilla PHP. When I try to use the extension, I get something like: PHP Warning:…
eli
  • 645
  • 7
  • 15
8
votes
3 answers

Renaming Functions during runtime in PHP

In PHP 5.3 is there a way to rename a function or "hook" a function. There is the rename_function() within "APD" which has been broken since ~2004. If you try and build it on PHP 5.3 you'll get this error: 'struct _zend_compiler_globals' has no…
rook
  • 66,304
  • 38
  • 162
  • 239
8
votes
1 answer

What class entries should I use to throw non-default exceptions from a C extension?

In my C extension, I can throw a PHP exception to the calling function with zend_throw_exception. The first parameter to that function is a zend_class_entry that specifies what type of exception to throw. I know from the documentation in…
murgatroid99
  • 19,007
  • 10
  • 60
  • 95
8
votes
3 answers

How to return array from a PHP extension, without copying it in memory?

I'm developing a PHP-extension, where an object method needs to return an array zval. The method looks like: ZEND_METHOD(myObject, myMethod) { zval **myArrayProperty; if (zend_hash_find(Z_OBJPROP_P(getThis()), "myArrayProperty",…
Andrey Tserkus
  • 3,644
  • 17
  • 24
8
votes
2 answers

SEG Fault in PHP extension

I wrote a PHP extension to access functions in a static lib, I built PHP as a CGI, and everything seemed to work (after days of working on it..) Thrilled once everything worked, I re-compiled PHP without debugging messages I had in it.…
Matt
  • 1,928
  • 24
  • 44
7
votes
2 answers

PHP http_build_url() and PECL Install

I downloaded an API that uses this php function http_build_url(). Everytime I run my application it says: Fatal error: Call to undefined function http_build_url() I made a research and found out that I should install pecl_http to run that…
PinoyStackOverflower
  • 5,214
  • 18
  • 63
  • 126
7
votes
2 answers

How to compile php extension for PHP 5.3.6 with debug, TS

I've tried to create some php extension. For the first I compiled php 5.3.6 with --enable-debug --enable-maintainer-zts. Then I ceated my extension and use standart procedure for it phpize ./configure --with-EXTNAME make Then copy extname.so to my…
krasilich
  • 643
  • 2
  • 9
  • 14