// 項目使用 `composer`
// 重新封裝了 redis
use cache\Redis;
// 因為封裝了幾種緩存方式 如:file,memcache
// 所以想要這種 字符串 的方式來 new 類
// 但是這個方式直接報錯,沒有重名問題
$class = 'Redis';
$instance = new $class($options);
// 如果直接 new,就沒有問題,可以正常運行
$instance = new Redis($options);
Der erste Fehler ist so PHP Fatal error: Class 'Redis' not found
.
Wenn ich das automatische Laden von Namespaces nicht verwende und include file
verwende, gibt es mit der ersten und zweiten Methode kein Problem.
Was ist das Prinzip und wie kann man es l?sen? Danke.
Following the voice in heart.
使用命名空間,以變量為類名實例化的時候,需要包含完整的命名空間
,在實例化的地方直接加命名空間
$cls_name = 'Redis';
$class = "\cache\Redis\\".$cls_name;
$instance = new $class($options);
需要完整的命名空間
use cache\Redis;
$class = Redis::class;//需要完整的命名空間
$instance = new $class($options);
OR
$class = '\cache\Redis';
//$class = \cache\Redis::class;
$instance = new $class($options);