语言和开发规则: Difference between revisions

Jump to navigation Jump to search
Line 445: Line 445:


==代码框架==
==代码框架==
为了使代码标准化并加快Dolibarr中新组件的开发,可以在 '''htdocs/modulebuilder/templates''' 目录中找到。
为了使代码标准化并加快Dolibarr中新组件的开发,可以在 '''htdocs/modulebuilder/templates''' 目录中找到准备好的框架文件。


例如:
例如:
Line 453: Line 453:
*创建新页面的代码示例  '''skeleton_page.php'''
*创建新页面的代码示例  '''skeleton_page.php'''
*创建要在命令行上运行的脚本的代码示例 '''skeleton_script.php'''
*创建要在命令行上运行的脚本的代码示例 '''skeleton_script.php'''
*...


请注意,PHP代码生成器([[Module ModuleBuilder|ModuleBuilder模块]])也使用这些框架,该模块在Dolibarr模块开发章节中有描述,用以提高您的开发速度。
请注意,PHP代码生成器([[Module ModuleBuilder|ModuleBuilder模块]])也使用这些框架,该模块在Dolibarr模块开发章节中有描述,用以提高您的开发速度。
Line 471: Line 472:


==UTF8/ISO 编码==
==UTF8/ISO 编码==
Dolibarr stores data in the following way:
Dolibarr 按以下方式存储数据:


*In database, data is stored in UTF8 or ISO. It depends on the database's pagecode therefore these options are set at creation time. In any case, Dolibarr's database driver (in lib/database) deals with it to convert from/to UTF8 at insertion and readout.
*在数据库中,数据为 UTF8 或 ISO 格式。这取决于数据库的pagecode(页面代码),本选项是在创建数据库时设置的。无论如何,Dolibarr的数据库访问驱动程序(在 lib/database 中)在读取和插入时处理UTF8的转换。
*Memory data is stored in UTF8 (PHP object instances).
*内存数据存储为 UTF8(PHP 对象的实例)格式。
*Screen displayed web pages are UTF8 (for versions prior to 2.5.1, the deprecated $character_set parameter from conf.php file defines output format).
*屏幕上显示的网页采用 UTF8 格式。


==浮点数,金额和计算==
==浮点数,金额和计算==
With PHP, like other languages (Java for exemple), non integer data (float, real, double) are not reliable for calculation.
在PHP中,就像在其他语言(如Java)中一样,非整数数据(float、real、double)对于计算来说是不可靠的。 例如:
Try to make for example
 
<syntaxhighlight lang="PHP">
<syntaxhighlight lang="PHP">
print 239.2 - 229.3 - 9.9;
print 239.2 - 229.3 - 9.9;
</syntaxhighlight>
</syntaxhighlight>
You wont get zero but a very small decimal number. If you get zero her, you should be able to find other examples that don't work.
 
Problem of float is general, so a variable that is a result of a calculation using decimal numbers must ALWAYS be cleaned using the function '''price2num()''' with the econd parameter to: 'MU', 'MT' or 'MS' depending on need (see description of function).
你不会得到零,而是一个非常小的负10的幂。如果你得到零,你可以找到其他不起作用的例子。
 
浮点数的问题是一般性的,计算实数的结果变量必须由 '''price2num()''' 函数系统地清理,根据需要将第二个参数设置为“MU”、“MT”或“MS”(参见函数文档)。
 
<syntaxhighlight lang="PHP">
<syntaxhighlight lang="PHP">
print price2num(239.2 - 229.3 - 9.9, 'MT');
print price2num(239.2 - 229.3 - 9.9, 'MT');
</syntaxhighlight>
</syntaxhighlight>
If data manipulated is not an amount, then using MU, MT, MS has no sense, and you must use the function '''round()'''.
 
如果计算的数据不是一个价格,那么使用 MU、MT、MS 就没有意义了,您必须使用 '''round()''' 函数。


==表的创建==
==表的创建==
Do not create tables, on the fly, during execution by a standard user, we mean, during current usage of software.
不要在使用时创建表,即'''首次使用'''模块时。如果您正在创建的模块使用的表不是 Dolibarr 代码中的标准表,请确保遵循 [[Module development|模块开发]] 教程,该教程解释了如何在模块激活时创建表,而不是在模块使用期间创建表(见上文)。
If you create a module that uses its own table, not available in the default Dolibarr code, then take a look at tutorial [[Module development]]. It explains how to provide new tables with your module, so that, the tables will be created during module activation and not during module usage.


==比较版本==
==比较版本==
If your code need to make different things depending on Dolibarr version, you can use the following tip to detect and compare versions
如果您的代码需要根据 Dolibarr 版本的不同而执行不同的操作,您可以使用以下技巧来检测和比较版本:
 
<syntaxhighlight lang="PHP">
<syntaxhighlight lang="PHP">
$version=preg_split('/[\.-]/',DOL_VERSION);
$version=preg_split('/[\.-]/',DOL_VERSION);
Line 501: Line 506:
</syntaxhighlight>
</syntaxhighlight>


But this solution need to include the function versioncompare. An alternative solution to test version is to do:
但是这个解决方案需要 include 函数 <syntaxhighlight lang="PHP">
$version=preg_split('/[\.-]/',DOL_VERSION);
if (versioncompare($version,array(5,0,-4)) >= 0) { //mycode for 5.0 only; } // For dolibarr 5.0.* (the -4 means we include also alpha, beta, rc and rcX)
</syntaxhighlight>。另一种替代解决方案是执行以下比较操作:
 
<syntaxhighlight lang="PHP">
<syntaxhighlight lang="PHP">
if ((float) DOL_VERSION >= 5.0) { //mycode for 5.0 only; } // For dolibarr 5.0.*
if ((float) DOL_VERSION >= 5.0) { //mycode for 5.0 only; } // For dolibarr 5.0.*
714

edits

Navigation menu