Updating PHP with custom configuration options - php

Let's suppose i have built php with ./configure --enable-safe-mode --disable-cgi etc. Is it possible to update php without loosing current configuration options? I know i could save them into a text file and bla bla but i am interesting in modifying php source files so i could compile ONLY php modules that i need. (eg. I don't need gd, tar, pgsql, mcrypt - functions that i'll never use. Why should i compile them?)
(sorry for bad english, not from uk/us)

As far as I know, what you're going for isn't possible. That said, you can see the command line options that your version of PHP was build with in the phpinfo() output, so you could just copy and paste it across.
Other than that, use a package manager! :)

Related

Compile PHP using shared option and directory location

I cannot figure out the proper ./configure options to use when giving the configure command if I would like to have something be shared (thus creating an .so extension) as well as stating a directory where required libraries are.
Example:
--with-openssl=shared will create a .so file
--with-openssl=/home/username/local will compile directly into the php build.
How do I state both? I see this nowhere in the php docs.
I am installing on a linux Ubuntu.
Well, I found it through trial and error as well as a few other posts of people going through different troubles that had their ./configure commands visible.
In my example case there are two flags you need:
--with-openssl=shared --with-openssl-dir=/home/username/local
In other instances there is no extension-name-dir flag, so you do comma separated values:
--with-cron=shared,/home/username/local
I could find no easy way to tell you which ones are which, but trial and error got me through these two that I needed. In the case of GD you actually need as many as four separate ones for the needed library support:
--with-gd=shared --with-jpeg-dir=/home/username/usr --with-png-dir=/home/username/usr --with-freetype-dir=/home/username/usr
Hope this helps someone else, I could find no other discussions about this issue (hard to believe there was no SO answers already).

PHP --with-curl

I need to install cURL. PHP's official website writes:
To use PHP's cURL support you must also compile PHP --with-curl[=DIR] where DIR is the location of the directory containing the lib and include directories.
But I've seen people doing it like that:
<?php
// some text...
--with-curl
// some text...
?>
Which one should I choose?
Writing --with-curl arbitrarily in the middle of your code does nothing except potentially cause syntax errors.
The code that you show does not make any sense, is not valid PHP code and the PHP interpreter will throw an error. If you want to use curl you have to install it.
The second option isn't valid, as others have mentioned. The only way to add modules (in this case, the one that adds curl support) is to compile PHP with those modules active. That is what --with-php does - tells PHP to compile with curl.
Now, the source code for some packages do allow you to add that to one of the compiling config files, and it often takes the same form as the command line switch. I'm not sure if PHP has that option (never needed to compile it by hand), but if it does, it's not going to be in a PHP file.
If you run a version of Linux, you also have the option of installing php-curl/php5-curl from your distribution's repository. Doing it that way saves the headache of compiling it yourself and remembering what you need to turn on and off, and handles dependency needs.
Ultimately, though, how you go about installing it or adding modules depends on your platform, skill level, and overall needs and comfort level.

Can you use scons to build PHP extensions?

The standard way of writing PHP extensions is to use autoconf/automake alongside a script called phpize, which seems to generate your autoconf configuration based on a template that's specific to your PHP environment. This let's it build the PHP extension for the right version of PHP, etc.
autoconf and the m4 language that is used to configure it is arcane, and people have written alternatives, such as scons. I want to be able to use one of these when building a PHP extension.
In principle, you should be able to use scons or similar tools to build PHP extensions. However, I can't see how you would replace the phpize step.
Has anyone had any success in building PHP extensions with scons, or another more modern build tool?
The path of least resistance would be to have SCons run autoconf, phpize and whatever else is needed for your PHP extension. You may be able to extract the compiler configuration out of there and let SCons do the actual building, or you can simply have SCons run "make".
Declaring shell command targets from SCons is easy, but getting dependencies right is always tricky.
Basically you will have to let SCons know of any intermediate file produced by these external tools. This way it can not only properly clean them, but it can also cache the whole series of steps based on the content signature of each intermediate result (MD5 checksum).
Proper caching will significantly reduce the number of times these external tools will actually need to be invoked as the code base changes.
While I don't think somebody has written a specific solution for PHP, there are lots of custom builders on the SCons wiki that do similar things.
phpize(1) is just a shell script, so i guess you could modify it to work with scons...

Extending PHP with C++?

I have a performance intensive routine that is written in PHP that I'd like to port to C++ for a performance increase. Is there any way to write a plugin or extension or something using C++ and interface with it from PHP? WITHOUT manually editing the actual PHP source?
As Remus says, you can extend PHP with C/C++ using the Zend API. The linked tutorial by Sara Golemon is a good start, and the book Extending and Embedding PHP by the same author covers the subject in much more detail.
However, it's worth noting that both of these (and pretty much everything else I found online) focus on C and don't really cover some tweaks you need to get C++ extensions working.
In the config.m4 file you need to explicitly link to the C++ standard library:
PHP_REQUIRE_CXX()
PHP_ADD_LIBRARY(stdc++, 1, PHP5CPP_SHARED_LIBADD)
Any C++ library compile checks in the config.m4 file will also require linking the C++ lib:
PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,,
[
AC_MSG_ERROR([lib $LIBNAME not found.])
],[
-lstdc++ -ldl
])
EDIT - and here's how to specify g++:
Last, and not least, in order to choose the C++ rather than C compiler/linker when building the extension, the 6th parameter to PHP_NEW_EXTENSION() should be "yes". ie:
PHP_NEW_EXTENSION(your_extension,
your_extension.cpp,
$ext_shared,
,
"-Wall -Werror -Wno-error=write-strings -Wno-sign-compare",
"yes")
From the PHP build system manual, the parameters are:
The name of the extension
List of all source files which are part of the extension.
(optional) $ext_shared, a value which was determined by configure when PHP_ARG_WITH() was called for
(optional) "SAPI class", only useful for extensions which require the CGI or CLI SAPIs specifically. It should be left empty in all other cases.
(optional) A list of flags to be added to CFLAGS while building the extension.
(optional) A boolean value which, if "yes", will force the entire extension to be built using $CXX instead of $CC.
I couldn't work out how to get the configure script to set g++ as the compiler/linker instead of gcc, so ended up hacking the Makefile with a sed command to do a search replace in my bash build script:
phpize
./configure --with-myextension
if [ "$?" == 0 ]; then
# Ugly hack to force use of g++ instead of gcc
# (otherwise we'll get linking errors at runtime)
sed -i 's/gcc/g++/g' Makefile
make clean
make
fi
Presumably there's an automake command that would make this hack unnecessary.
I've written a PHP plugin in C++ with the help of SWIG. It's doable, but it may take a while to get used to the SWIG-compilation cycle. You can start with the SWIG docs for PHP.
Update
As #therefromhere has mentioned, I greatly recommend that you get the book Extending and Embedding PHP. There is almost no documentation to be found online (at least there wasn't in late 2008, early 2009 when I did my PHP plugin). I had to rely on the book for everything. Although sometimes Google Code Search is helpful for finding sample code.
PHP itself a collection of loosely related libraries. See http://devzone.zend.com/article/1021 for a tutorial how to write your own.

xgettext vs gettext

I have a few questions:
I know what gettext is. I've read a few posts where they mentioned xgettext and was curious as to what is the difference between the two.
How can I install xgettext on Windows?
And finally, does anybody have a tutorial on how to install the library php-gettext http://savannah.nongnu.org/projects/php-gettext/ (this one usually doesn't come with PHP) I've read about it in an article but I'm not sure how to get it working in Windows. The thing is, sometimes when you make changes, you need to restart Apache to see the new data with the gettext that comes with PHP (but with the library you don't need to restart it) so I wanted to use the library for development. Thanks!
In regards to the question:
I know what gettext is. I've read a few posts where they mentioned xgettext and was curious as to what is the difference between the two.
In short, gettext() is a function and xgettext is a utility program for extracting messages from source code.
In long, SO answer to Complete C++ i18n gettext() “hello world” example shows as part of the C++ source code file hellogt.cxx:
gettext("hello, world!")
The gettext() function is passed a text string that is used as an index to the message to be used at run-time. It returns the specified message for the language which is specified either in the code or at the time the program is invoked.
Then it shows:
xgettext --package-name hellogt --package-version 1.2 --default-domain hellogt --output hellogt.pot hellogt.cxx
which is a utility program used at build time to examine the source code file hellogt.cxx for text strings passed to gettext(). These are extracted and used to create the Portable Object Template file hellogt.pot.
The .pot file template is used by translators in the process of delivering the binary translated message file hellogt.mo used at run-time by gettext().
Install Cygwin and select the gettext-devel package.
This will install the xgettext.exe
The Zend Framework has a gettext Zend_Translate adapter that doesn't require the php gettext extention.
xgettext is part of gettext, it's a program that extracts translatable strings from program sources. See gettext's manual.
I don't know about its availability on Windows, Google tells me there's a port.
The online function reference reference tells me there is no xgettext.
Maybe they mean one of
ngettext dgettext dngettext dcgettext dcngettext
treating the 'x' like a wildcard

Categories