我想創建一個類並擴展一個PHP類FilesystemIterator,如下面的代碼所示。我定義了一個hasFlag()方法並測試它是否包含一個標誌(我希望它像其他PHP函數,例如glob),但結果總是與預期不同。那麼我該如何解決這個問題呢?如何使用功能標誌?
class c extends FilesystemIterator
{
/* These are parent constants */
const CURRENT_AS_FILEINFO = 0 ;
const KEY_AS_PATHNAME = 0 ;
const CURRENT_AS_SELF = 16 ;
const CURRENT_AS_PATHNAME = 32 ;
const CURRENT_MODE_MASK = 240 ;
const KEY_AS_FILENAME = 256 ;
const NEW_CURRENT_AND_KEY = 256 ;
const FOLLOW_SYMLINKS = 512 ;
const KEY_MODE_MASK = 3840 ;
const SKIP_DOTS = 4096 ;
const UNIX_PATHS = 8192 ;
public function __construct($flags) {
$this->flags = $flags;
}
public function hasFlag($flag) {
//How do I test $this->flags it contains a $flag???
return ($this->flags & $flag) ? true : false;
}
}
$c = new c(
c::CURRENT_AS_FILEINFO |
c::KEY_AS_PATHNAME |
c::CURRENT_AS_SELF |
c::CURRENT_AS_PATHNAME |
c::CURRENT_MODE_MASK |
c::KEY_AS_FILENAME |
c::NEW_CURRENT_AND_KEY |
c::FOLLOW_SYMLINKS |
c::KEY_MODE_MASK |
c::SKIP_DOTS |
c::UNIX_PATHS
);
var_dump($c->hasFlag(c::CURRENT_AS_FILEINFO));
EDIT 1
爲什麼如此??
var_dump(((0 | 16 | 32 | 240 | 3840) & 0) == 0); //true
var_dump(((0 | 16 | 32 | 240 | 3840) & 32) == 32); //true
var_dump(((0 | 16 | 32 | 240 | 3840) & 240) == 240); //true
var_dump(((0 | 16 | 32 | 240 | 3840) & 1024) == 1024); //true??
var_dump(((0 | 16 | 32 | 240 | 3840) & 2048) == 2048); //true??
var_dump(((0 | 16 | 32 | 240 | 3840) & 3840) == 3840); //true
我知道我只是告訴你 – Jasper 2013-05-04 14:31:21
啊好吧,不是很確定 – hakre 2013-05-04 14:32:04
'$ c-> hasFlag(c :: CURRENT_AS_FILEINFO)'預計爲'false'。 – 2013-05-04 14:41:57