Make PEAR IT-templates work on a PHP 7 server

Some of my older PHP programs use PEAR-IT-templates. At the core of this PHP template engine is the “IT.php” file defining a class “HTML_Template_IT” via an old fashioned “contructor” function of the same name (stemming from old PHP4’s way to define classes).

Yesterday, I needed to make some IT-templates work on an Apache2 server with PHP 7.1. This was easier than expected: Setting PHP to display all warnings and errors leads directly to the right code statements which must be corrected:

Correcting the call of preg_replace

In a first step we can ignore the warning about the old fashioned class declaration, which will sooner or later disappear from PHP. What absolutely needs to be changed, however, is an error that arises due to a special usage of the function preg_replace():

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback

In my PEAR IT-installation the problem is located around line 956 of the file “IT.php”. Fortunately, some other clever people have already provided a method, how to replace the critical statement:

        // Replace the following lines by the subsequent statements
        /*
        return preg_replace(
            "#<!-- INCLUDE (.*) -->#ime", "\$this->getFile('\\1')", $content
        );
        */
	return preg_replace_callback(
	   "#<!-- INCLUDE (.*) -->#im",
	    array($this, 'getFile'),
	    $content
	);

See the contribution of A. Preuss in the following stack overflow post:
http://stackoverflow.com/questions/22597102/preg-replace-the-e-modifier-is-deprecated-error

Worked perfectly for me. Many thanks to A. Preuss!

Changing to a __construct() function

To get rid of the warning regarding the old fashioned class constructor function just replace the function definition around line 376 by

    // function HTML_Template_IT($root = "", $options=null) {
    function __construct($root = "", $options=null) {

With both the replacements described above I could use my old IT-template dependent PHP programs successfully on a PHP 7.1 system! This, hopefully, gives me enough time to replace the PEAR IT engine by the Smarty engine wherever reasonable :-).