1. Go to this page and download the library: Download kode/core 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/ */
kode / core example snippets
Kode\Core\App;
use Kode\Core\Config\Config;
use Kode\Core\Provider\ServiceProvider;
// 1) 业务服务
final class Greeter
{
public function __construct(private string $name) {}
public function hello(): string { return "Hello, {$this->name}!"; }
}
// 2) 服务提供者
final class AppServiceProvider extends ServiceProvider
{
public function register(): void
{
$name = $this->container->make(Config::class)->get('app.name', 'Kode');
$this->container->singleton(Greeter::class, fn() => new Greeter($name));
}
}
// 3) 一行启动
$app = App::boot([
'base_path' => __DIR__,
'config_path' => __DIR__ . '/config', // 目录下的 *.php 自动加载为配置
'providers' => [AppServiceProvider::class],
'runtime' => ['fiber'], // 启用命令:叠加协程运行时
]);
echo $app->make(Greeter::class)->hello(); // Hello, Kode!
echo $app->config('app.name'); // Kode
use Kode\Core\Provider\ServiceProvider;
final class CacheServiceProvider extends ServiceProvider
{
public function register(): void
{
// 注册阶段:登记绑定(其它服务可能尚未就绪)
$this->container->singleton(\Psr\SimpleCache\CacheInterface::class, RedisCache::class);
}
public function boot(): void
{
// 启动阶段:依赖已就绪,可做初始化(连接、订阅等)
}
}