PHP: change attribute from inherited class
maybe some of you use jpgraph to generate some charts. I want to change the private attribute ($errwidth) from an jpgraph-class ( ErrorPlot ). In most cases jpgraph provides an function to set all nessesary properties. But not in this case.
Here my try:
class ErrorPlot extends Plot {
// Original class
private $errwidth=2;
...
}
class SpecialErrorPlot extends ErrorPlot {
// "Extension" to modify the attribute
function SetErrWidth( $w ){
$this->errwidth = $w;
}
}
$chart_obj = new SpecialErrorPlot( array(1,2,3,4,5,6) );
$chart_obj->SetErrWidth(10);
SpecialErrorPlot Object (
[errwidth:private] =>开发者_Go百科; 2
...
[errwidth] => 10
)
The result: 2 attributes! Bad enough to get these 2 attributes, but i don't understand why!?!? i hope you can help me!
Stefan
you want protected not private.
protected function and variables can be modified by child classes whereas private functions and variables cannot be modified except by that class alone.
You should look at Reflection - setAccessible()
精彩评论