PHPには、クラスが存在するのに、class_existsで検出できないピット..
798 ワード
質問:
クラスが存在するのに、現在のクラスの定義でclass_を使用してもexistsは、現在のクラスが存在するかどうかを検出してfalseを返します.
次のようになります.
ソリューション:
元はネーミングスペースを使用した後、現在のクラス定義のために簡略化されることなく、完全なネーミングスペース付きクラス名を使用する必要があります.
次のようになります.
クラスが存在するのに、現在のクラスの定義でclass_を使用してもexistsは、現在のクラスが存在するかどうかを検出してfalseを返します.
次のようになります.
namespace amsx\account;
class ActiveCodeProcessor{
private static $_processerPool = [];
/**
* @param $type
* @return ActiveCodeProcessor
*/
public static function getCodeProcessorObj($type){
if(!key_exists($type, self::$_processerPool)){
$className = 'ActiveCodeProcessor'.$type;
if(class_exists($className)){
self::$_processerPool[$type] = new $className;
}
}
//......
}
}
ソリューション:
元はネーミングスペースを使用した後、現在のクラス定義のために簡略化されることなく、完全なネーミングスペース付きクラス名を使用する必要があります.
次のようになります.
echo class_exists('\think\Cache');