[置顶] PHP5 面向对象的一个新疑惑点,类对象实例竟然能访问类中私有方法,不知道是不是bug

来源:互联网 发布:华为编程大赛题目 编辑:程序博客网 时间:2024/06/07 18:32

先看代码:

class orm_factory {/** * 数据库操作对象 * * @var orm_driver */private $_driver = null ;/** * 表操作对象 * * @var orm_table_operator */private $_table_operator = null ;private static $_valid_dsn_set = array();private function __init_params(orm_driver $driver,orm_table_operator $table_operator){$this->_driver = $driver ;echo "111111111111" ;$this->_table_operator = $table_operator ;}function get_driver(){return $this->_driver ;}/** * 工厂实例对象 * @param string $dsn * @return orm_factory */static function instance($dsn){static $inst = null ;if (!$inst) $inst = new orm_factory() ;if (!isset(self::$_valid_dsn_set[$dsn])){try {$driver = new orm_driver($dsn) ;$driver->open_connect();$table_operator = new orm_table_operator($driver);self::$_valid_dsn_set[$dsn] = array('__driver__' => $driver ,'__operator__' => $table_operator);} catch (orm_driver_exception $ex){throw new orm_factory_exception($ex->getMessage());}}$inst->__init_params(self::$_valid_dsn_set[$dsn]['__driver__'],self::$_valid_dsn_set[$dsn]['__operator__']);return $inst ;}/** * 返回表操作器对象 * @param string $table * @return orm_table_operator */function get_operator($table){// 此处 可能触发 orm_table_exception$this->_table_operator->register_table($table) ;return $this->_table_operator;}}

 
加亮处 应该是有问题的

$inst->__init_params

 此处竟然能从对象实例中访问私有方法,太不可思议了.... 难不成是因为在一个类中的代码都是可以调用的,而不是以 对象 来单独划分的么?

 

0 0
原创粉丝点击