最新の PHP フレームワークで IOC コンテナを理解して実装するための 1 つの記事
この記事では、PHP に関する関連知識をお届けします。主に IOC コンテナに関する関連コンテンツを紹介します。IOC の正式名称は、Inversion Of Control、反転制御です。一緒に話しましょう。見てください、それが皆さんのお役に立てば幸いです。
<?php class Sunny{ } $sunny = new Sunny();
<?php class Sunny{ } $sunny = Container::getBean(Sunny::class);
<?php class Computer{ public function run(){ echo "编程中....\n"; } } class Sunny{ private $computer; public function __construct(Computer $computer){ $this->computer = $computer; } public function program(){ $this->computer->run(); } } $sunny = new Sunny(new Computer()); $sunny->program();
<?php class Computer{ public function run(){ echo "编程中....\n"; } } class Sunny{ private $computer; public function __construct(Computer $computer){ $this->computer = $computer; } public function program(){ $this->computer->run(); } } $sunny = Container::getBean(Sunny::class); $sunny->program();
class Cache{ public function set($key,$value){ $redis = new CFile(); $redis->set($key,$value); } } class CFile{ public function set($key,$value){ echo "file:{$key}->{$value}\n"; } } $cache = new Cache(); $cache->set("name","sunny");
class Cache{ public function set($key,$value){ $redis = new CRedis(); $redis->set($key,$value); } } class CRedis{ public function set($key,$value){ echo "redis:{$key}->{$value}\n"; } } $cache = new Cache(); $cache->set("name","sunny");
interface ICache{ public function set($key,$value); } class CRedis implements ICache { public function set($key,$value) { echo "redis:{$key}->{$value}\n"; } } class CFile implements ICache{ public function set($key,$value) { echo "file:{$key}->{$value}\n"; } } class Cache{ private $drive; public function __construct(ICache $drive) { $this->drive = $drive; } public function set($key,$value){ $this->drive->set($key,$value); } } $cache = new Cache(new CFile()); $cache->set("name","sunny");
<?php
class Container
{
// 当前容器对象
private static $instance;
// 存放在容器里面到实例
protected $instances = [];
private function __construct()
{
}
public static function getInstance()
{
if (!self::$instance) {
self::$instance = new static();
}
return self::$instance;
}
/**
* 获取对象实例
* @param $key
* @return mixed
*/
public function get($key)
{
if (isset($this->instances[$key])) {
return $this->instances[$key];
}
}
/**
* 绑定对象、闭包、类到容器
* @param $key
* @param null $concrete
* @return Container
*/
public function bind($key, $concrete = null)
{
if ($concrete instanceof Closure) {
$this->instances[$key] = $concrete;
} elseif (is_object($concrete)) {
$this->instances[$key] = $concrete;
}
return $this;
}
}
class Sunny
{
public function getName()
{
echo time() . "\n";
}
}
$app = Container::getInstance();
$sunny = $app->bind(Sunny::class,new Sunny());
$sunny = $app->get(Sunny::class);
$sunny->getName();
ログイン後にコピー
実践的な戦闘: 依存性の注入の実装<?php class Container { // 当前容器对象 private static $instance; // 存放在容器里面到实例 protected $instances = []; private function __construct() { } public static function getInstance() { if (!self::$instance) { self::$instance = new static(); } return self::$instance; } /** * 获取对象实例 * @param $key * @return mixed */ public function get($key) { if (isset($this->instances[$key])) { return $this->instances[$key]; } } /** * 绑定对象、闭包、类到容器 * @param $key * @param null $concrete * @return Container */ public function bind($key, $concrete = null) { if ($concrete instanceof Closure) { $this->instances[$key] = $concrete; } elseif (is_object($concrete)) { $this->instances[$key] = $concrete; } return $this; } } class Sunny { public function getName() { echo time() . "\n"; } } $app = Container::getInstance(); $sunny = $app->bind(Sunny::class,new Sunny()); $sunny = $app->get(Sunny::class); $sunny->getName();
Container.php
<?php
class Container
{
// 当前容器对象
private static $instance;
// 存放在容器里面到实例
protected $instances = [];
private function __construct()
{
}
public static function getInstance()
{
if (!self::$instance) {
self::$instance = new static();
}
return self::$instance;
}
/**
* 获取对象实例
* @param $key
* @return mixed
* @throws ReflectionException
*/
public function get($key)
{
if (isset($this->instances[$key])) {
return $this->instances[$key];
}
return $this->make($key);
}
/**
* 绑定对象、闭包、类到容器
* @param $key
* @param null $concrete
* @return Container
* @throws ReflectionException
*/
public function bind($key, $concrete = null)
{
if ($concrete instanceof Closure) {
$this->instances[$key] = $concrete;
} elseif (is_object($concrete)) {
$this->instances[$key] = $concrete;
} else {
$this->make($key, $concrete);
}
return $this;
}
/**
* 创建类绑定到类实例
* @param $abstract
* @param null $atgs
* @return mixed
* @throws ReflectionException
*/
public function make($abstract, $atgs = null)
{
if (isset($this->instances[$abstract])) {
return $this->instances[$abstract];
}
$object = $this->invokeClass($abstract);
$this->instances[$abstract] = $object;
return $object;
}
/**
* 反射解析类
* @param $abstract
* @return object
* @throws ReflectionException
*/
public function invokeClass($abstract)
{
$reflectionClass = new \ReflectionClass($abstract);
// 获取构造方法
$construct = $reflectionClass->getConstructor();
// 获取参数得到实例
$params = $construct ? $this->parserParams($construct) : [];
$object = $reflectionClass->newInstanceArgs($params);
return $object;
}
/**
* 解析构造方法参数
* @param $reflect
* @return array
* @throws ReflectionException
*/
public function parserParams(ReflectionMethod $reflect)
{
$args = [];
$params = $reflect->getParameters();
if (!$params) {
return $args;
}
if (count($params) > 0) {
foreach ($params as $param) {
$class = $param->getClass();
if ($class) {
$args[] = $this->make($class->getName());
continue;
}
// 获取变量的名称
$name = $param->getName();
// 默认值
$def = null;
// 如果有默认值,从默认值获取类型
if ($param->isOptional()) {
$def = $param->getDefaultValue();
}
$args[] = $_REQUEST[$name] ?? $def;
}
}
return $args;
}
}
Test.php
<?php
class Test
{
public $name;
private $test1;
public function __construct(Test1 $test1)
{
$this->test1 = $test1;
$this->name = $this->test1->getName();
}
}
Test1.php
<?php
class Test1
{
public function getName(){
return "test1返回的名字";
}
}
Sunny.php
<?php
require_once "./Container.php";
require_once "./Test.php";
require_once "./Test1.php";
class Sunny
{
private $test;
public function __construct(Test $test)
{
$this->test = $test;
}
public function getName()
{
echo "获取test里面的name:{$this->test->name}\n";
}
}
$app = Container::getInstance();
$sunny = $app->get(Sunny::class);
$sunny->getName();
ログイン後にコピー推奨学習: 「
Container.php <?php class Container { // 当前容器对象 private static $instance; // 存放在容器里面到实例 protected $instances = []; private function __construct() { } public static function getInstance() { if (!self::$instance) { self::$instance = new static(); } return self::$instance; } /** * 获取对象实例 * @param $key * @return mixed * @throws ReflectionException */ public function get($key) { if (isset($this->instances[$key])) { return $this->instances[$key]; } return $this->make($key); } /** * 绑定对象、闭包、类到容器 * @param $key * @param null $concrete * @return Container * @throws ReflectionException */ public function bind($key, $concrete = null) { if ($concrete instanceof Closure) { $this->instances[$key] = $concrete; } elseif (is_object($concrete)) { $this->instances[$key] = $concrete; } else { $this->make($key, $concrete); } return $this; } /** * 创建类绑定到类实例 * @param $abstract * @param null $atgs * @return mixed * @throws ReflectionException */ public function make($abstract, $atgs = null) { if (isset($this->instances[$abstract])) { return $this->instances[$abstract]; } $object = $this->invokeClass($abstract); $this->instances[$abstract] = $object; return $object; } /** * 反射解析类 * @param $abstract * @return object * @throws ReflectionException */ public function invokeClass($abstract) { $reflectionClass = new \ReflectionClass($abstract); // 获取构造方法 $construct = $reflectionClass->getConstructor(); // 获取参数得到实例 $params = $construct ? $this->parserParams($construct) : []; $object = $reflectionClass->newInstanceArgs($params); return $object; } /** * 解析构造方法参数 * @param $reflect * @return array * @throws ReflectionException */ public function parserParams(ReflectionMethod $reflect) { $args = []; $params = $reflect->getParameters(); if (!$params) { return $args; } if (count($params) > 0) { foreach ($params as $param) { $class = $param->getClass(); if ($class) { $args[] = $this->make($class->getName()); continue; } // 获取变量的名称 $name = $param->getName(); // 默认值 $def = null; // 如果有默认值,从默认值获取类型 if ($param->isOptional()) { $def = $param->getDefaultValue(); } $args[] = $_REQUEST[$name] ?? $def; } } return $args; } } Test.php <?php class Test { public $name; private $test1; public function __construct(Test1 $test1) { $this->test1 = $test1; $this->name = $this->test1->getName(); } } Test1.php <?php class Test1 { public function getName(){ return "test1返回的名字"; } } Sunny.php <?php require_once "./Container.php"; require_once "./Test.php"; require_once "./Test1.php"; class Sunny { private $test; public function __construct(Test $test) { $this->test = $test; } public function getName() { echo "获取test里面的name:{$this->test->name}\n"; } } $app = Container::getInstance(); $sunny = $app->get(Sunny::class); $sunny->getName();
PHP ビデオ チュートリアル 」
以上が最新の PHP フレームワークで IOC コンテナを理解して実装するための 1 つの記事の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック











PHP 8.4 では、いくつかの新機能、セキュリティの改善、パフォーマンスの改善が行われ、かなりの量の機能の非推奨と削除が行われています。 このガイドでは、Ubuntu、Debian、またはその派生版に PHP 8.4 をインストールする方法、または PHP 8.4 にアップグレードする方法について説明します。

あなたが経験豊富な PHP 開発者であれば、すでにそこにいて、すでにそれを行っていると感じているかもしれません。あなたは、運用を達成するために、かなりの数のアプリケーションを開発し、数百万行のコードをデバッグし、大量のスクリプトを微調整してきました。

Visual Studio Code (VS Code とも呼ばれる) は、すべての主要なオペレーティング システムで利用できる無料のソース コード エディター (統合開発環境 (IDE)) です。 多くのプログラミング言語の拡張機能の大規模なコレクションを備えた VS Code は、

JWTは、JSONに基づくオープン標準であり、主にアイデンティティ認証と情報交換のために、当事者間で情報を安全に送信するために使用されます。 1。JWTは、ヘッダー、ペイロード、署名の3つの部分で構成されています。 2。JWTの実用的な原則には、JWTの生成、JWTの検証、ペイロードの解析という3つのステップが含まれます。 3. PHPでの認証にJWTを使用する場合、JWTを生成および検証でき、ユーザーの役割と許可情報を高度な使用に含めることができます。 4.一般的なエラーには、署名検証障害、トークンの有効期限、およびペイロードが大きくなります。デバッグスキルには、デバッグツールの使用とロギングが含まれます。 5.パフォーマンスの最適化とベストプラクティスには、適切な署名アルゴリズムの使用、有効期間を合理的に設定することが含まれます。

文字列は、文字、数字、シンボルを含む一連の文字です。このチュートリアルでは、さまざまな方法を使用してPHPの特定の文字列内の母音の数を計算する方法を学びます。英語の母音は、a、e、i、o、u、そしてそれらは大文字または小文字である可能性があります。 母音とは何ですか? 母音は、特定の発音を表すアルファベットのある文字です。大文字と小文字など、英語には5つの母音があります。 a、e、i、o、u 例1 入力:string = "tutorialspoint" 出力:6 説明する 文字列「TutorialSpoint」の母音は、u、o、i、a、o、iです。合計で6元があります

このチュートリアルでは、PHPを使用してXMLドキュメントを効率的に処理する方法を示しています。 XML(拡張可能なマークアップ言語)は、人間の読みやすさとマシン解析の両方に合わせて設計された多用途のテキストベースのマークアップ言語です。一般的にデータストレージに使用されます

静的結合(静的::) PHPで後期静的結合(LSB)を実装し、クラスを定義するのではなく、静的コンテキストで呼び出しクラスを参照できるようにします。 1)解析プロセスは実行時に実行されます。2)継承関係のコールクラスを検索します。3)パフォーマンスオーバーヘッドをもたらす可能性があります。

PHPの魔法の方法は何ですか? PHPの魔法の方法には次のものが含まれます。1。\ _ \ _コンストラクト、オブジェクトの初期化に使用されます。 2。\ _ \ _リソースのクリーンアップに使用される破壊。 3。\ _ \ _呼び出し、存在しないメソッド呼び出しを処理します。 4。\ _ \ _ get、dynamic属性アクセスを実装します。 5。\ _ \ _セット、動的属性設定を実装します。これらの方法は、特定の状況で自動的に呼び出され、コードの柔軟性と効率を向上させます。
