关于public protected function 的一些想法
最近在用CodeIgniter框架写一些东西的时候想到,为什么CodeIgniter文档中没有见到除了public以外的声明呢?是不是应该尽量使用protected的声明呢。
但是如果我把之前写的一些函数改成protected声明之后又会出错。当我在网上搜索一些相关情况的时候我找了这样一段话
Technically, it is possible to invoke private and protected methods using the reflection API. However, 99% of the time doing so is a really bad idea. If you can modify the class, then the correct solution is probably to just make the method public. After all, if you need to access it outside the class, that defeats the point of marking it protected.
从技术上讲,在别的地方调用私有和受保护的方法是可能的。但是这是一个糟糕的决定。毕竟,如果你需要在类之外访问它,那就没有必要把它标记为受保护的。
我想了想,觉得非常有道理。或者说我之前的想法很糟糕。我既然要在别的地方访问它,那么我把它声明为受保护的又有什么意义呢?
评论已关闭