PHP:更改继承类的属性
也许你们中的一些人使用 jpgraph 来生成一些图表。我想更改 jpgraph 类( ErrorPlot )的私有属性($errwidth)。在大多数情况下,jpgraph 提供了一个设置所有必要属性的函数。但在本例中并非如此。
这是我的尝试:
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] => 2
...
[errwidth] => 10
)
结果:2 个属性!获得这两个属性已经够糟糕了,但我不明白为什么!?!?我希望你能帮助我!
斯特凡
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] => 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你想要受保护而不是私有。
受保护的函数和变量可以由子类修改,而私有函数和变量只能由该类单独修改。
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.
您应该查看反射 - setAccessible()
You should look at Reflection - setAccessible()