Home » Database » DB_DataObject » Bug #10229
Improved PHP 4 handling in generator
Details
| Request #10229 | Improved PHP 4 handling in generator |
|---|---|
| Submitted | 2007-03-01 10:55 UTC |
| From | lboshell at php dot net |
| Status | Feedback |
| Package | DB_DataObject |
| PHP Version | 5.1.6 |
| OS | Linux |
| Roadmaps | (Not assigned) |
Comments
[2007-03-01 10:55 UTC] lboshell at php dot net
Description:
------------
Hello,
The generator is a bit limited at the moment when it is used on a system with PHP 5, but the results are intended to be used on PHP 4.
Bug #5146 improved things adding the generator_var_keyword option, but there's still a couple of problems with it:
* There are other cases in the Generator where different syntax is created depending on the expression substr(phpversion(),0,1), instead of using the config parameter.
* It's not very well documented.
Below are two diffs I created to replace 'generator_var_keyword' with 'generator_php4_syntax', use it consistently in the Generator, and document it in the manual and the .ini example.
Thanks.
====
DB_DataObject-1.8.5-force_php4.patch
====
diff -NurdpB DB_DataObject-1.8.5/DataObject/Generator.php DB_DataObject-1.8.5-patched/DataObject/Generator.php
--- DB_DataObject-1.8.5/DataObject/Generator.php 2006-10-15 21:20:24.000000000 -0500
+++ DB_DataObject-1.8.5-patched/DataObject/Generator.php 2007-03-01 05:32:45.000000000 -0500
@@ -18,7 +18,8 @@
* @version CVS: $Id: Generator.php,v 1.122 2006/10/16 02:20:16 alan_k Exp $
* @link http://pear.php.net/package/DB_DataObject
*/
-
+
+
/*
* Security Notes:
* This class uses eval to create classes on the fly.
@@ -164,6 +165,26 @@ class DB_DataObject_Generator extends DB
var $_newConfig;
/**
+ * Checks if PHP 4 syntax needs to be used when generating code
+ * Users of this class can customize this behaviour using the
+ * 'generator_php4_syntax' config parameter.
+ *
+ * @access private
+ * @return true if php 4 syntax has to be used, false otherwise
+ */
+ function _php4_syntax()
+ {
+ $options = &PEAR::getStaticProperty('DB_DataObject','options');
+
+ if ((isset($options['generator_php4_syntax']) &&
+ $options['generator_php4_syntax']) ||
+ substr(phpversion(),0,1) < 5)
+ return true;
+
+ return false;
+ }
+
+ /**
* Build a list of tables;
* and store it in $this->tables and $this->_definitions[tablename];
*
@@ -797,8 +818,7 @@ class DB_DataObject_Generator extends DB
$options = &PEAR::getStaticProperty('DB_DataObject','options');
- $var = (substr(phpversion(),0,1) > 4) ? 'public' : 'var';
- $var = !empty($options['generator_var_keyword']) ? $options['generator_var_keyword'] : $var;
+ $var = ($this->_php4_syntax()) ? 'var' : 'public';
$body .= " {$var} \$__table = '{$this->table}'; {$p}// table name\n";
@@ -847,7 +867,7 @@ class DB_DataObject_Generator extends DB
// and replace them with $x = clone($y);
// due to the change in the PHP5 clone design.
- if ( substr(phpversion(),0,1) < 5) {
+ if ($this->_php4_syntax()) {
$body .= "\n";
$body .= " /* ZE2 compatibility trick*/\n";
$body .= " function __clone() { return \$this;}\n";
@@ -1146,7 +1166,7 @@ class DB_DataObject_Generator extends DB
: " * @return {$t->type}\n";
$getters .= " * @access public\n";
$getters .= " */\n";
- $getters .= (substr(phpversion(),0,1) > 4) ? ' public '
+ $getters .= (! $this->_php4_syntax()) ? ' public '
: ' ';
$getters .= "function $methodName() {\n";
$getters .= " return \$this->{$t->name};\n";
@@ -1198,7 +1218,7 @@ class DB_DataObject_Generator extends DB
$setters .= " * @param mixed input value\n";
$setters .= " * @access public\n";
$setters .= " */\n";
- $setters .= (substr(phpversion(),0,1) > 4) ? ' public '
+ $setters .= (! $this->_php4_syntax()) ? ' public '
: ' ';
$setters .= "function $methodName(\$value) {\n";
$setters .= " \$this->{$t->name} = \$value;\n";
diff -NurdpB DB_DataObject-1.8.5/docs/example.ini DB_DataObject-1.8.5-patched/docs/example.ini
--- DB_DataObject-1.8.5/docs/example.ini 2006-07-25 22:44:51.000000000 -0500
+++ DB_DataObject-1.8.5-patched/docs/example.ini 2007-03-01 05:36:40.000000000 -0500
@@ -81,11 +81,10 @@ class_prefix = DataObjects_
; (True) prevents writing of private/var's so you can overload get/set
; note: this has the downside of making code less clear... (alot of magic!!)
-;generator_var_keyword = 'var'
- ; var|public - (or private if you want to break things)
- ; The variable prefix that is used when class properties are created
- ; the default is public for PHP5, and var for PHP4
-
+;generator_php4_syntax = true
+ ; (True) causes the generator to use PHP 4 syntax ('var' keyword
+ ; instead of 'public' for properties and so on) even if
+ ; the PHP version used at runtime is PHP 5 or better
;generator_add_validate_stubs =
; (True) will insert / (or add to existing files) stubs for validate methods
===
intro-configuration.xml.patch
(For the manual, in peardoc/en/package/database/db-dataobject)
===
--- intro-configuration.xml 2007-03-01 05:38:38.000000000 -0500
+++ intro-configuration.xml.new 2007-03-01 05:44:23.000000000 -0500
@@ -550,6 +550,21 @@
</listitem>
</varlistentry>
+ <varlistentry>
+ <term>
+ <parameter>generator_php4_syntax</parameter>
+ <type>boolean</type>
+ </term>
+ <listitem>
+ <para>
+ If True, the generator will use PHP 4 syntax when creating
+ classes (<literal>var</literal> instead of
+ <literal>public</literal> in property declarations and so on).
+ This is useful if you use the generator on a system with PHP
+ 5, and you want to deploy the result on a system with PHP 4.
+ </para>
+ </listitem>
+ </varlistentry>
</variablelist>
</refsect1>