cvs: peardoc /en/package/php/php-compatinfo advanced.xml

From: Date: Sat, 05 Jul 2008 15:53:51 +0000
Subject: cvs: peardoc /en/package/php/php-compatinfo advanced.xml
Groups: php.pear.doc 
Request: Send a blank email to pear-doc+get-10945@lists.php.net to get a copy of this message
farell Sat Jul 5 15:53:51 2008 UTC Added files: /peardoc/en/package/php/php-compatinfo advanced.xml Log: third chapter of tutorial (advanced detection) -inital commit http://cvs.php.net/viewvc.cgi/peardoc/en/package/php/php-compatinfo/advanced.xml?view=markup&rev=1.1 Index: peardoc/en/package/php/php-compatinfo/advanced.xml +++ peardoc/en/package/php/php-compatinfo/advanced.xml <!-- $Revision: 1.1 $ --> <refentry id="package.php.php-compatinfo.advanced"> <refnamediv> <refname>Advanced detection</refname> <refpurpose>parse data source with additional options</refpurpose> </refnamediv> <refsect1 id="package.php.php-compatinfo.advanced.file"> <title>Detection of a single file</title> <para> If your file implement code condition that is optional and don't break main goal, such as, for example : if <methodname>function_exists</methodname> then I do something, else I do something else. </para> <simpara> Solution is very easy: You have to specify what function required should be considered as optional. </simpara> <para> Suppose we have to detect which PHP version we need to run this chunk of script named &quot;errorHandler.php&quot;. With standard behavior, PCI returns PHP 4.3.0 (because <methodname>debug_backtrace</methodname> came with version 4.3.0). So, if we ignore function <methodname>debug_backtrace</methodname> to find out the minimum version, we will get the real and true result. </para> <para> <programlisting role="php"> <![CDATA[ <?php // ... if (function_exists('debug_backtrace')) { $backtrace = debug_backtrace(); } else { $backtrace = false; } // ... ?> ]]> </programlisting> </para> <para> We will use another very simple detection script. Have a look on options array given as second parameter (here is the magic). <programlisting role="php"> <![CDATA[ <?php require_once 'PHP/CompatInfo.php'; $source = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'errorHandler.php'; $options = array('ignore_functions' => array('debug_backtrace')); $info = new PHP_CompatInfo(); $info->parseFile($source, $options); // you may also use unified method: $info->parseData($source); ?> ]]> </programlisting> </para> <para> And displayed results are : <screen> array ( 'ignored_files' => array ( ), 'ignored_functions' => array ( 0 => 'debug_backtrace', ), 'ignored_extensions' => array ( ), 'ignored_constants' => array ( ), 'max_version' => '', 'version' => '4.0.0', 'extensions' => array ( ), 'constants' => array ( ), 'tokens' => array ( ), 'cond_code' => array ( 0 => 1, ), ) </screen> that means chunk of script named &quot;errorHandler.php&quot; need PHP 4.0.0, have condition code (function condition : cond_code = 1), and php <ulink url="&url.php;debug_backtrace">debug_backtrace</ulink> function was excluded from scope. </para> <para> <note> <para> Since version 1.7.0, you may catch this situation (more easily), and exclude from scope all functions that are conditionned by a <emphasis>function_exists</emphasis>. See example that follow. </para> </note> </para> <para> Other alternative is to use <emphasis role="bold">ignore_functions_match</emphasis> option. <programlisting role="php"> <![CDATA[ <?php require_once 'PHP/CompatInfo.php'; $source = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'errorHandler.php'; $options = array('ignore_functions_match' => array('function_exists', array('/.*/'))); $info = new PHP_CompatInfo(); $info->parseFile($source, $options); // you may also use unified method: $info->parseData($source); ?> ]]> </programlisting> </para> <para> The string <emphasis role="bold">function_exists</emphasis> as first parameter, tell to ignore function(s) that match only conditionnal code with php function_exists(). </para> <para> The other possibility is string <emphasis role="bold">preg_match</emphasis>, that give more freedom, and catch function that match the pattern (without condition). </para> <para> While array as second parameter, gave a list of pattern (function name) that must be catch and ignored. </para> </refsect1> </refentry> <!-- Keep this comment at the end of the file Local variables: mode: sgml sgml-omittag:t sgml-shorttag:t sgml-minimize-attributes:nil sgml-always-quote-attributes:t sgml-indent-step:1 sgml-indent-data:t sgml-parent-document:nil sgml-default-dtd-file:"../../../../manual.ced" sgml-exposed-tags:nil sgml-local-catalogs:nil sgml-local-ecat-files:nil End: vim600: syn=xml fen fdm=syntax fdl=2 si vim: et tw=78 syn=sgml vi: ts=1 sw=1 -->

« previous php.pear.doc (#10945) next »