unit testing - How do I access private attributes in PHP? -


possible duplicate:
best practices test protected methods phpunit

class footer {   private $_isenabled;    public function __construct(){     $this->_isenabled = true;   }    public function disable(){     $this->_isenabled = false;   }   } 

when writing unit test disable function after set _iseanabled false, want assert whether or not false.

but how can access $_isenabled?

this test function:

public function testdisable(){   $footer = new footer();   $footer->disable();   $this->assertfalse($footer->_isenable); } 

short answer:

you cannot. that's private means...

long answer:

you can using reflection:

http://php.net/manual/en/book.reflection.php

it bit complicated, though, , adds layer prone fail not best way testing...

i rather prefer create getter function:

public function getenabled() {    return $this->_isenabled; } 

but if have not done simple, think not want create it... given alternatives, may reconsider it.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c++ - Using OpenSSL in a multi-threaded application -

All overlapping substrings matching a java regex -