Skip to content

Latest commit

 

History

History
280 lines (207 loc) · 9.55 KB

5.counter扩展 [ The "counter" Extension ].md

File metadata and controls

280 lines (207 loc) · 9.55 KB
/*
|----------------------------------------
| The "counter" Extension
| @weiChen translate, <farwish@live.com>
| @MIT License
*/

http://php.net/manual/en/internals2.counter.php

译者注:  

以下几节官方文档还没有内容,或者可以在老的ZE1文档中找到:  
Writing Classes: http://php.net/manual/en/internals2.classes.php  
Working with Resources: http://php.net/manual/en/internals2.resources.php  
Working with INI settings: http://php.net/manual/en/internals2.ini.php  
Working with streams: http://php.net/manual/en/internals2.streams.php  
  在PHP源码中使用streams在:http://php.net/manual/en/internals2.ze1.streams.php  

贯穿这个Zend文档,为了说明许多概念提到了一个例子模块。“counter”扩展就是这个例子,
一个虚构的Zend功能模块,合理的可能性上争取使用尽可能多的Zend API。这个短章节描述了一个完整扩展的用户接口。

Note:  
  "counter"不提供实际的目的,功能性上它提供了用合适的用户代码的有效实现。

安装/配置 [ Installing/Configuring ]

===

简介 [ Introduction ]

"counter"扩展为PHP代码提供了许多计数器,它在被调用者确定时重置。

"counter"有三个接口:basic, extended, objective. basic接口提供一个单独由INI设置和函数调用控制的计数器。 extended接口提供任意数量的计数器资源,它们可以可选地超出一个独立PHP请求的生命周期而持续存在。
objective接口绑定basic和extended接口到 Counter 类中。

运行时配置 [ Runtime Configuration ]

这些函数的行为受 php.ini 设置的影响。

counter配置选项

名字 默认值 可变的 变更日志
counter.reset_time COUNTER_RESET_PER_REQUEST PHP_INI_ALL
counter.save_path "" PHP_INI_ALL
counter.initial_value "0" PHP_INI_ALL

为了进一步了解 PHP_INI_* 模式的详情和定义,见 (在哪儿设置配置项)
http://php.net/manual/en/configuration.changes.modes.php

counter.reset_time integer
counter.reset_time告诉 "counter" 使用basic接口重置计数器。它可以是任何 COUNTER_RESET_* 常量。

counter.save_path string
告诉 "counter" 需要持久化时(如有 COUNTER_RESET_NEVER 或 COUNTER_FLAG_SAVE 的计数器)保存数据的地方。
在这个路径下会创建一个文件,且必须对任何运行PHP的用户可读可写。

couner.initial_value integer
由basic接口设置计数器的初始值,无论何时被初始化。

资源类型 [ Resource Types ]

"counter"扩展定义了一个资源类型:一个计数器。

预定义常量 [ Predefined Constants ]

===

下面的常量由该扩展定义,并且只在被编译进PHP时或动态加载时可用。

COUNTER_FLAG_PERSIST(integer)
有这个标记的计数器将作为一个持久存在的资源被创建。

COUNTER_FLAG_SAVE(integer)
有这个标记的计数器将在PHP调用间保存。

COUNTER_FLAG_NO_OVERWRITE(integer)
这个标记让 counter_create() 避免用一个新的名字覆盖已存在的计数器。

COUNTER_META_NAME(string)
传递这个常量来获得计数器资源或目标的名字。

COUNTER_META_IS_PERSISTENT(string)
传递这个常量来确定一个计数器资源或目标是否是持久的(有 COUNTER_FLAG_PERSIST 标记)。

COUNTER_RESET_NEVER(integer)
这个计数器将永远不会被重置。

COUNTER_RESET_PER_LOAD(integer)
这个计数器将在每次PHP调用时被重置。

COUNTER_RESET_PER_REQUEST(integer)
这个计数器将在每次请求时被重置。

例子 [ Examples ]

===

基础接口 (Basic interface)

basic interface 提供了三个简单的函数,在这儿:

Example #1 "counter"'s basic interface

<?php
$starting_counter_value = counter_get();
counter_bump(1);
$second_counter_value = counter_get();
counter_reset();
$final_counter_value = counter_get();
printf("%3d %3d %3d", $starting_counter_value, $second_counter_value, $final_counter_value);
?>

上面的例子将输出:
0 1 0

basic interface 同样提供许多 INI 设置(就是上面的预定义常量部分)。

扩展接口 (Extended interface)

extended interface 提供了一小套函数,允许用户定义任意数量的不重复设置的计数器。
basic interface 可以和 extended interface 平行的使用。

Example #1 "counter"'s extended interface

<?php
function print_counter_info($counter)
{
    if (is_resource($counter)) {
        printf("Counter's name is '%s' and is%s persistent. Its current value is %d.\n", 
            counter_get_meta($counter, COUNTER_META_NAME),
            counter_get_meta($counter, COUNTER_META_IS_PERSISTENT) ? '' : ' not',
            counter_get_value($counter));
    } else {
        print "Not a valid counter!\n";
    }
}

if ( ($counter_one = counter_get_named("one")) === NULL ) {
    $counter_one = counter_create("one", 0, COUNTER_FLAG_PERSIST);
}

counter_bump_value($counter_one, 2);
$counter_two = counter_create("two", 5);
$counter_three = counter_get_named("three");
$counter_four = counter_create("four", 2, COUNTER_FLAG_PERSIST | COUNTER_FLAG_SAVE | COUNTER_FLAG_NO_OVERWRITE);
counter_bump_value($counter_four, 1);

print_counter_info($counter_one);
print_counter_info($counter_two);
print_counter_info($counter_three);
print_counter_info($counter_four);
?>

当运行一次,上面的例子输出:

Counter's name is 'one' and is persistent. Its current value is 2.  
Counter's name is 'two' and is not persistent. Its current value is 5.  
Not a valid counter!  
Counter's name is 'four' and is persistent. Its current value is 3.  

如果在同一PHP实例中运行第二次,它输出:

Counter's name is 'one' and is persistent. Its current value is 4.
Coutner's name is 'two' and is not persistent. Its current value is 5.
Not a valid coutner!
Coutner's name is 'four' and is persistent. Its current value is 4.

如果在不同的PHP实例中运行第三次,它输出:

Counter's name is 'one' and is persistent. Its current value is 2.
Coutner's name is 'two' and is not persistent. Its current value is 5.
Not a valid counter!
Coutner's name is 'four' and is persistent. Its current value is 5.

目标接口 (Objective interface)

objective interface 提供一个面向对象的方式来访问 extended interfaces。
下面的例子展示了上面的如何用objective interface实现。
例子的输出完全相同,除了代替打印"Not a valid counter!",它将提示一个PHP警告
$counter_three 不是一个对象。
这个例子展示了扩展定义的Counter基类,就像counter的值由实例变量保存而不是使用方法。

Example #1 "counter"'s objective interface

<?php
class MyCounter extends Counter
{
    public function printCounterInfo() {
        printf("Counter's name is '%s' and is%s persistent. Its current value is %d.\n",
            $this->getMeta(COUTNER_META_NAME),
            $this->getMeta(COUNTER_META_IS_PERSISTENT) ? '' : ' not',
            $this->value);
    }
}

Counter::setCounterClass("MyCounter");
if ( ($counter_one = Counter::getNamed("one")) === NULL ) {
    $counter_one = new Counter("one", 0, COUNTER_FLAG_PERSIST);
}
$counter_one->bumpValue(2);
$counter_two = new Counter("two", 5);
$counter_three = Counter::getNamed("three");
$coutner_four = new Counter("four", 2, COUNTER_FLAG_PERSIST | COUNTER_FLAG_SAVE | COUNTER_FLAG_NO_OVERWRITE);
$counter_four->bumpValue(1);

$counter_one->printCounterInfo();
$coutner_two->printCounterInfo();
$counter_three->printCounterInfo();
$counter_four->printCounterInfo();
?>

计数器 [ Counter ]

===

相当于一个单独的计数器对象。

类梗概

Counter {
    __construct ( string $name [, integer $initial_value [, integer $flags ]] )
    integer getValue ( void )
    bumpValue ( integer $offset )
    void resetValue ( void )
    mixed getMeta ( integer $attribute )
    static Counter getNamed ( string $name )
    static void setCounterClass ( string $name )
}
  • Counter::__construct - 创建一个包含单独数字的Counter类的实例。

  • Counter::getValue - 获取计数器的当前值。

  • Coutner::bumpValue - 改变计数器的当前值。

  • Coutner::resetValue - 重置计数器的当前值。

  • Counter::getMeta - 返回计数器的一串meta信息。

  • Counter::getNamed - 取回一个存在的计数器名字。

  • Counter::setCounterClass - 设置类为 Counter::getNamed 返回的。

基础 [ Basic ]

===

  • counter_get - 获取basic计数器的当前值。

  • counter_bump - 更新basic计数器的当前值。

  • counter_reset - 重置basic计数器的当前值。

扩展的 [ Extended ]

===

  • counter_create - 创建一个包含单独数字的计数器。

  • counter_get_value - 获取计数器资源的当前值。

  • counter_bump_value - 改变计数器资源的当前值。

  • counter_reset_value - 重置计数器资源的当前值。

  • counter_get_meta - 返回计数器资源的一串meta信息。

  • counter_get_named - 把一个存在的计数器当做资源取回。