php 源码解析-凯发k8官方网
凯发k8官方网
收集整理的这篇文章主要介绍了
php 源码解析--count
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
php内置了许多处理array的函数,这些函数功能简单,但组合使用能够形成强大的功能,非常好用~~
在php 源代码中,array相关的内置函数定义在/ext/standard/php_array.h和/ext/standard/array.c中,主要的提供的函数如下:
php_function(ksort); php_function(krsort); php_function(natsort); php_function(natcasesort); php_function(asort); php_function(arsort); php_function(sort); php_function(rsort); php_function(usort); php_function(uasort); php_function(uksort);由于偶也是初学,请见谅~~,先挑软柿子捏,看看最简单的count函数的实现
我们知道php中提供的count函数的形式是count(mixed var [, int mode]),后面这个参数表示是否recursive true表示递归计算,否则则只算第一层,
比方说count(array('a'=>'1',‘b'=>array(‘c’=>2))=2;而count(array('a'=>'1',‘b'=>array(‘c’=>2),true)=3
php_function(count) {zval *array;long mode = count_normal; //解析参数,z|l的形式表示第二个参数可选,php内部默认为count_normalif (zend_parse_parameters(zend_num_args() tsrmls_cc, "z|l", &array, &mode) == failure) { return;} switch (z_type_p(array)) { case is_null: //count(null)这种直接返回1return_long(0); break; case is_array: //count(array(...))会递归计算return_long (php_count_recursive (array, mode tsrmls_cc)); break; case is_object: {#ifdef have_splzval *retval;#endif/* first, we check if the handler is defined *///这里没看明白~~后面补上if (z_obj_ht_p(array)->count_elements) {retval_long(1); if (success == z_obj_ht(*array)->count_elements(array, &z_lval_p(return_value) tsrmls_cc)) { return;}}#ifdef have_spl//count(obj)//如果obj实现了spl中的countable接口,即实现了count函数,则会调用这个函数,并用这个函数的输出作为最终的count的返回值/*** class test implements countable{* public function count(){* return 5;* }* }* $test=new test();* 那么count($test)=5*//* if not and the object implements countable we call its count() method */if (z_obj_ht_p(array)->get_class_entry && instanceof_function(z_objce_p(array), spl_ce_countable tsrmls_cc)) {zend_call_method_with_0_params(&array, null, null, "count", &retval); if (retval) {convert_to_long_ex(&retval);retval_long(z_lval_p(retval));zval_ptr_dtor(&retval);} return;}#endif} default: //其他的一概返回1,比方说//count("hello world")=1//count(123)=1等等return_long(1); break;} }转载于:https://blog.51cto.com/7814265/1793465
总结
以上是凯发k8官方网为你收集整理的php 源码解析--count的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: jquery lightbox图片放大预
- 下一篇: uwp composition api