PHP code example of 96qbhy / hyid

1. Go to this page and download the library: Download 96qbhy/hyid 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/ */

    

96qbhy / hyid example snippets


class User extends Model{
    use Qbhy\Hyid\HyidAble;
    
    // or 
    public function getUserId($userId){
        return hyid($userId);
    }
    
    // or
    public function toArray(){
        $data = parent::toArray();

        $data['id'] = hyid()->encode($data['id']);
        
        return $data;
    }
}
// decode

 public function userinfo($id){
        return User::query()->findOrFail(hyid()->decode($id))->toArray();
 }

// 非 laravel or lumen 下,可以自行实例化 Hyid 类
$secret = 'qbhy';
$offset = 1996;
$hyid = new Hyid($secret,$offset,4);

$encodedId = $hyid->encode(1);
$id = $hyid->decode($encodedId); // 1
text
HYID_SECRET=qbhy
HYID_OFFSET=1996
HYID_RANDOM_LENGTH=6