summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOlly Betts <olly@survex.com>2021-03-23 11:49:57 +1300
committerOlly Betts <olly@survex.com>2021-03-23 11:52:00 +1300
commit338cdd068530e653a7200581cb834a74f71d457f (patch)
treecade2e71d2c1ec804bb9c24611c0c04deca7c1b0
parent9df60351e45055ea882d837af0de28ae95a0ee3e (diff)
downloadswig-338cdd068530e653a7200581cb834a74f71d457f.tar.gz
php: Stop using dl()
With modern PHP it only works with the CLI version of PHP, so it's better to direct users to load the extension via "extension=" in php.ini. Suggested by ferdynator in #1529.
-rw-r--r--Doc/Manual/Php.html21
-rw-r--r--Examples/Makefile.in3
-rw-r--r--Examples/test-suite/php/Makefile.in4
-rw-r--r--Source/Modules/php.cxx13
4 files changed, 13 insertions, 28 deletions
diff --git a/Doc/Manual/Php.html b/Doc/Manual/Php.html
index 1752168b2..0c09c54dc 100644
--- a/Doc/Manual/Php.html
+++ b/Doc/Manual/Php.html
@@ -152,9 +152,9 @@ If the module is in PHP's default extension directory, you can omit the path.
</p>
<p>
-For some SAPIs (for example, the CLI SAPI) you can instead use the
-<a href="https://www.php.net/manual/en/function.dl.php">dl() function</a> to load
-an extension at run time, by adding a line like this to the start of each
+If you're using the PHP CLI SAPI it's possible (but not recommended) to use the
+<a href="https://www.php.net/manual/en/function.dl.php">dl() function</a> to
+load an extension at run time, by adding a line like this to the start of each
PHP script which uses your extension:
</p>
@@ -163,25 +163,22 @@ PHP script which uses your extension:
</pre></div>
<p>
-But note that <tt>dl()</tt> isn't supported when running PHP through a
-webserver - you'll need to use <tt>extension</tt> in <tt>php.ini</tt> as
+But to do this portably you need to take into account that pathnames and the
+filename extension vary by platform, and for security reasons PHP no longer
+supports <tt>dl()</tt> when running PHP through a webserver. Overall it's
+probably better to instead use <tt>extension</tt> in <tt>php.ini</tt> as
described above.
</p>
<p>
-The PHP module which SWIG generates will also attempt to do the <tt>dl()</tt>
-call for you if the extension isn't already loaded:
+SWIG also generates a PHP module which defines PHP classes for the wrapped
+API, which you'll need to load, for example:
</p>
<div class="code"><pre>
include("example.php");
</pre></div>
-<p>
-This PHP module also defines the PHP classes for the wrapped API, so you'll
-almost certainly want to include it anyway.
-</p>
-
<H2><a name="Php_nn2">32.2 Basic PHP interface</a></H2>
diff --git a/Examples/Makefile.in b/Examples/Makefile.in
index 838350921..3372b52c2 100644
--- a/Examples/Makefile.in
+++ b/Examples/Makefile.in
@@ -1052,6 +1052,7 @@ PHP = @PHP@
PHP_INCLUDE = @PHPINC@
PHP_SO = @PHP_SO@
PHP_SCRIPT = $(SRCDIR)$(RUNME).php
+PHP_EXTENSION = `pwd`
# -------------------------------------------------------------------
# Build a PHP dynamically loadable module (C)
@@ -1076,7 +1077,7 @@ php_cpp: $(SRCDIR_SRCS)
# -----------------------------------------------------------------
php_run:
- $(RUNTOOL) $(PHP) -n -d extension_dir=. -d display_errors=stderr $(PHP_SCRIPT) $(RUNPIPE)
+ $(RUNTOOL) $(PHP) -n -d extension_dir=. -d extension=$(PHP_EXTENSION) -d display_errors=stderr $(PHP_SCRIPT) $(RUNPIPE)
# -----------------------------------------------------------------
# Version display
diff --git a/Examples/test-suite/php/Makefile.in b/Examples/test-suite/php/Makefile.in
index 693615bc6..370dc58c0 100644
--- a/Examples/test-suite/php/Makefile.in
+++ b/Examples/test-suite/php/Makefile.in
@@ -62,9 +62,9 @@ missingtests: missingcpptests missingctests
# found, runs testcase.php, except for multicpptests.
run_testcase = \
if [ -f $(SCRIPTDIR)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX) ]; then \
- $(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile PHP_SCRIPT=$(SCRIPTDIR)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX) RUNTOOL='$(RUNTOOL)' php_run; \
+ $(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile PHP_EXTENSION=$(TARGETPREFIX)$*$(PHP_SO) PHP_SCRIPT=$(SCRIPTDIR)/$(SCRIPTPREFIX)$*$(SCRIPTSUFFIX) RUNTOOL='$(RUNTOOL)' php_run; \
elif [ -f $(SCRIPTDIR)/$(SCRIPTPREFIX)$*.php -a ! -f $(top_srcdir)/$(EXAMPLES)/$(TEST_SUITE)/$*.list ]; then \
- $(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile PHP_SCRIPT=$(SCRIPTDIR)/$(SCRIPTPREFIX)$*.php RUNTOOL='$(RUNTOOL)' php_run; \
+ $(MAKE) -f $(top_builddir)/$(EXAMPLES)/Makefile PHP_EXTENSION=$(TARGETPREFIX)$*$(PHP_SO) PHP_SCRIPT=$(SCRIPTDIR)/$(SCRIPTPREFIX)$*.php RUNTOOL='$(RUNTOOL)' php_run; \
fi
# Clean: remove the generated .php file
diff --git a/Source/Modules/php.cxx b/Source/Modules/php.cxx
index d8ee75b45..ae3e800d6 100644
--- a/Source/Modules/php.cxx
+++ b/Source/Modules/php.cxx
@@ -343,19 +343,6 @@ public:
Swig_banner(f_phpcode);
Printf(f_phpcode, "\n");
- Printf(f_phpcode, "// Try to load our extension if it's not already loaded.\n");
- Printf(f_phpcode, "if (!extension_loaded('%s')) {\n", module);
- Printf(f_phpcode, " if (strtolower(substr(PHP_OS, 0, 3)) === 'win') {\n");
- Printf(f_phpcode, " if (!dl('php_%s.dll')) return;\n", module);
- Printf(f_phpcode, " } else {\n");
- Printf(f_phpcode, " // PHP_SHLIB_SUFFIX gives 'dylib' on MacOS X but modules are 'so'.\n");
- Printf(f_phpcode, " if (PHP_SHLIB_SUFFIX === 'dylib') {\n");
- Printf(f_phpcode, " if (!dl('%s.so')) return;\n", module);
- Printf(f_phpcode, " } else {\n");
- Printf(f_phpcode, " if (!dl('%s.'.PHP_SHLIB_SUFFIX)) return;\n", module);
- Printf(f_phpcode, " }\n");
- Printf(f_phpcode, " }\n");
- Printf(f_phpcode, "}\n\n");
/* sub-sections of the php file */
pragma_code = NewStringEmpty();