2017-07-17 47 views
0

现在我有一个关于如何查看sprintf php funciton的源代码的问题。因为我想知道函数在哪里以及如何执行。 我可以通过grep -r 'PHP_FUNCTION(round)'找到一些函数,如math.c中的圆函数,但是sprintf与循环函数不相似。php sprintf函数的php源代码

+1

https://github.com/php/php-src/blob/c8aa6f3a9a3d2c114d0c5e0c9fdd0a465dbb54a5/ext/standard/formatted_print.c#L662 –

+0

非常感谢这个简单的问题,我应该用grep“sprintf的”。 – GenialX

+0

'grep PHP_FUNCTION(user_sprintf)'猜测前缀是因为C已经有一个名字相同的函数 – apokryfos

回答

2

有两种很好的方法来查看PHP源代码。

首先最简单的就是使用你的IDE。例如在Netbeans ctrl+right click上的任何函数都会带你到它的源头。这种方法毫无疑问是最快的,编写代码时,它是了解函数如何工作的最简单方法。

但是,您不一定会获得完整的源代码。以sprintf为例,您只需获得以下内容。

/** 
* (PHP 4, PHP 5, PHP 7)<br/> 
* Return a formatted string 
* @link http://php.net/manual/en/function.sprintf.php 
* @param string $format <p> 
* The format string is composed of zero or more directives: 
* ordinary characters (excluding %) that are 
* copied directly to the result, and conversion 
* specifications, each of which results in fetching its 
* own parameter. This applies to both <b>sprintf</b> 
* and <b>printf</b>. 
* </p> 
* <p> 
* Each conversion specification consists of a percent sign 
* (%), followed by one or more of these 
* elements, in order: 
* An optional sign specifier that forces a sign 
* (- or +) to be used on a number. By default, only the - sign is used 
* on a number if it's negative. This specifier forces positive numbers 
* to have the + sign attached as well, and was added in PHP 4.3.0. 
* @param mixed $args [optional] 
* @param mixed $_ [optional] 
* @return string a string produced according to the formatting string 
* <i>format</i>. 
*/ 
function sprintf(string $format, $args = null, $_ = null): string {} 

有关使用案例99%,我觉得上面的是绰绰有余了解函数是如何工作的,最重要的是,如何使用它。

另一种更简单的方法是结账PHP Git。下面是sprintf的实际来源:

#include <stdio.h> 
#include <stdarg.h> 
#include "php.h" 
#ifdef PHP_WIN32 
#include "config.w32.h" 
#else 
#include <php_config.h> 
#endif 

PHPAPI int 
php_sprintf (char*s, const char* format, ...) 
{ 
    va_list args; 
    int ret; 

    va_start (args, format); 
    s[0] = '\0'; 
    ret = vsprintf (s, format, args); 
    va_end (args); 
    return (ret < 0) ? -1 : ret; 
} 

来源:https://github.com/php/php-src/blob/master/main/php_sprintf.c

+0

您发布的源代码不是PHP ['sprintf()'](http://php.net/manual/en/function.sprintf.php)函数的代码。在PHP中没有'char *'这样的东西,PHP版本的'sprintf()'返回一个字符串,而不是一个数字。当标准C库不遵守C语言的C99标准时,这是PHP内部使用的'sprintf()'的替代实现,而不是标准C库提供的。 – axiac

+0

是不是所有的PHP核心都是C? – Doug

+0

PHP的确是用C编写的。解释器和PHP函数。我的观点是,当PHP脚本执行'sprintf(“...”)'时,您发布的代码不会运行。这段代码在解释器中运行,它也是许多PHP函数('password_hash()','addcslashes()'等)的实现的一部分,但不是作为* printf()PHP函数的一部分。 – axiac

1

PHP实现使用php_formatted_print()内部函数,它处理所有*printf() PHP函数(printf()sprintf()fprintf()vprintf()vsprintf()vfprintf())。他们都做类似的处理,只有输出的目的地不同。

C函数user_sprintf()(名为这种方式,以避免与由标准C库提供的sprintf()功能相冲突)被声明为sprintf() PHP functionthe implementation

Its code很简单(硬质的提升是通过php_formatted_print()执行):

/* {{{ proto string sprintf(string format [, mixed arg1 [, mixed ...]]) 
    Return a formatted string */ 
PHP_FUNCTION(user_sprintf) 
{ 
    zend_string *result; 

    if ((result=php_formatted_print(execute_data, 0, 0))==NULL) { 
     RETURN_FALSE; 
    } 
    RETVAL_STR(result); 
} 
/* }}} */ 

有实现其他*printf() PHP功能类似的功能。他们处理由php_formatted_print()返回的值的方式不同。

+0

公平竞赛。我认为**避免**阅读php_formatted_print()来源可能符合每个人的利益,除非您想要脑部疼痛。 'php_sprintf'和'zend_sprintf'服务的目的是什么? – Doug

+0

另外,如果你看看main/php.h,你会在第41行看到#undef sprintf #define sprintf php_sprintf /ext/standard/basic_functions.c中的源是否覆盖main/php.h中定义的内容? https://github.com/php/php-src/blob/6035ebd4a6c2c81eaf7b2ac71f6e23cb04e2c000/main/php.h#L41 – Doug

+0

不可以。“main”目录包含解释器的代码。 'ext'目录包含PHP扩展的实现; 'ext/standard'包含[核心扩展名](http://php.net/manual/en/extensions.membership.php#extensions.membership.core)的代码(除了在其中托管的'SPL'外自己的目录)。 'ext/standard/basic_functions.c'包含一个结构体列表('const zend_function_entry basic_functions []'),它告诉解释器C函数('user_sprintf')实现一个PHP函数('sprintf')以及它的参数列表如何显示像('arginfo_sprintf')。 – axiac