php - How to use function flags? -


i want create class , extend php class filesystemiterator in following code. define method hasflag() , test whether contains flag (i want other php functions, eg. glob), result different expected. how can fix problem?

class c extends filesystemiterator  {     /* these 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 test $this->flags contains $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

why ??

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 

about defaults , masks

there few special flags used in filesystemiterator, known masks; group multiple flags related (or in case, mutually exclusive) , should not passed regular flags; below binary representation:

000x00xx0000 +--++--+    |   |    |   +---- current_mode_mask    |    +-------- key_mode_mask 

those flags used determine whether default key() , current() methods should used. defaults both methods defined here:

const current_as_fileinfo  = 0 ; const key_as_pathname      = 0 ; 

the following code illustrates how test it:

if ($flags & current_mode_mask == 0) {     // current_as_fileinfo used } else {     // either current_as_pathname, current_as_self or current_as_pathname used }  if ($flags & key_mode_mask == 0) {     // key_as_pathname used } else {     // key_as_filename used } 

the problem having function such ->hasflag() can't differentiate between 2 defaults, i.e. want test current_as_fileinfo or key_as_pathname? have rethink logic.

about mutually exclusive flags

there few flags can't used result in undefined behaviour; example:

const current_as_self      = 16 ; const current_as_pathname  = 32 ; 

you can't define 2 types of behaviour current(), either 1 of them (or default) should used. compatible set of flags this:

$c = new c(     c::current_as_self |     c::key_as_filename  |     c::follow_symlinks |     c::skip_dots |     c::unix_paths ); 

about extending classes

assuming constructor same parent can remove constructor altogether:

class c extends filesystemiterator  {     public function hasflag($flag)     {         $flags = $this->getflags(); // use parent function here         // logic here     } } 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -