私はこのような状況があります。今Person_Object's id value 10
printIdInMapper()
で関数をエコーする方法をここに他のクラスのあるクラスの変数にアクセスする方法は?
// 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
を
作成'$ _id'のためのgetterと' $ this'を 'printI dInMapper 'そうだね。 –
[クラス外の保護されたメンバ変数へのアクセス]の可能な複製(http://stackoverflow.com/questions/3475601/accessing-a-protected-member-variable-outside-a-class) – outis