У меня есть ситуации, как это:Как получить доступ к переменной одного класса из другого класса?
// Object Class
class Person_Object {
protected $_id;
public function __construct($id = null) {
$this->_id = $id;
}
public function getMapper() {
$mapper = new Person_Mapper();
return $mapper;
}
public function printIdInMapper() {
$this->getMapper()->printIdInMapper();
}
}
// Mapper Class
class Person_Mapper {
public function printIdInMapper() {
// How to access Person_Object's id here and echo id?
}
}
// Code
$personModel = new Person_Object(10);
$personModel->printIdInMapper(); // should print 10
Теперь, как эхо Person_Object's id value 10
в printIdInMapper()
функция здесь
Создать getter для '$ _id' и передать' $ this' на 'printI dInMapper' Думаю. –
Возможный дубликат [Доступ к защищенной переменной-члену вне класса] (http://stackoverflow.com/questions/3475601/accessing-a-protected-member-variable-outside-a-class) – outis