PHP code example of pagon / fiber

1. Go to this page and download the library: Download pagon/fiber library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

pagon / fiber example snippets




$dic = new Fiber();

$dic = new Fiber(array(
    'db' => new Database();
));

$dic->db_host = 'localhost';
$dic->db_name => 'test';

$db_host = $dic->db_host;

// $dic也会做为参数传入

$dic->random = function ($dic) {
    return random();
};

$dic->time = function ($dic) {
    return time();
};

$random = $dic->random;
$time = $dic->time;


// 标准方式
$dic->db = $dic->share(function ($dic) {
    return new Database($dic->db_host);
});

// 键名定义方式
$dic->share('db', function ($dic) {
    return new Database($dic->db_host);
});

$db = $dic->db;

// 如果重新调用

$db1 = $dic->db // 这时候$db1和$db共享同一个数据库


// 标准定义
$dic->save = $dic->protect(function($key, $value){
    return $_SESSION[$key] = $value;
})

// 键名定义
$dic->protect('save', function($key, $value){
    return $_SESSION[$key] = $value;
})

// 获取函数再调用
$save = $dic->save;
$save('test', 'value');

// 直接调用
$dic->save('test', 'value');

$dic->extend('db', $dic->share(function ($db, $dic) {
    $db->select($dic->db_name);
    return $db;
}));