2010-08-05 125 views
0

我尝试加载一个实体的一些细节,并在实体中有一个resellerId。PHP的继承超类变量

现在我继承它的一个子类,并尝试访问resellerId但它不在那里。如何将属性传递给子类?我真的需要它加载。

谢谢!

编辑:

class Crm_Entity extends Crm_Abstract { 

    protected $_mapper = 'Crm_Mapper_Entity'; 
    protected $_data = array(
     'customerId'   => '',  //done 
     'displayName'   => '',  //done 
     'active'    => '',  //done 
     'billingMethod'   => '',  //done 
     'debitorNumber'   => null, //done 
     'bankAccount'   => null, //done 
     'bankAccountTown'  => null, //done 
     'resellerOrganisationId'=> null, 
     'account'    => null, //done 
     'phonenumbers'   => null, //done 
     'calls'     => null, 
     'tickets'    => null, 
     'comments'    => null, 
     'addresses'    => null, 
     'emailaddresses'  => null, //done 
     'actionevents'   => null 
    ); 
} 

class Crm_CustomerEntity extends Crm_Entity { 
    //foobar not done yet 
} 

class Crm_Person extends Crm_CustomerEntity { 

    protected $_data = array(
     'customerId' => null, 
     'firstName'  => '', 
     'middleName' => '', 
     'lastName'  => '', 
     'initials'  => '', 
     'title'   => '', 
     'openingWord' => '' 
    ); 
} 

所以我需要得到resellerId传递到子类。

+0

请显示一些代码,否则很难说出任何内容。 – enricog 2010-08-05 09:59:43

+0

你能提供更多细节吗?请添加负责定义类,属性,子类的类定义的片段;访问财产。 – 2010-08-05 10:00:09

+0

添加了一些代码,也许我在做一些非常愚蠢的事情...... – baklap 2010-08-05 10:18:41

回答

3

好了,这个现在很清楚。

您正在将数据存储在关联模式中。你正在重新定义这个数组的子类。当然,这样就不会有超类的价值。

将数组定义移至类构造函数中。那么你应该有

<?php 
class Crm_Entity extends Crm_Abstract 
{ 

    protected $_mapper = 'Crm_Mapper_Entity'; 

    protected $_data; 

    public function __construct() 
    { 
     parent::__construct(); 
     $newData = array(
      'customerId'   => '',  //done 
      'displayName'   => '',  //done 
      'active'    => '',  //done 
      'billingMethod'   => '',  //done 
      'debitorNumber'   => null, //done 
      'bankAccount'   => null, //done 
      'bankAccountTown'  => null, //done 
      'resellerOrganisationId'=> null, 
      'account'    => null, //done 
      'phonenumbers'   => null, //done 
      'calls'     => null, 
      'tickets'    => null, 
      'comments'    => null, 
      'addresses'    => null, 
      'emailaddresses'  => null, //done 
      'actionevents'   => null 
     ); 
     $this->_data = $newData; 
    } 
} 

class Crm_CustomerEntity extends Crm_Entity 
{ 
    //foobar not done yet 
    public function __construct() 
    { 
     parent::__construct(); 
    } 
} 

class Crm_Person extends Crm_CustomerEntity 
{ 
    public function __construct() 
    { 
     parent::__construct(); 

     $newData = array(
      'customerId' => null, 
      'firstName'  => '', 
      'middleName' => '', 
      'lastName'  => '', 
      'initials'  => '', 
      'title'   => '', 
      'openingWord' => '' 
     ); 
     $this->_data = array_merge($this->_data, $newData); 
    } 
} 

当然实际的设计取决于点 - 如果你想要这些映射创建类之前,你应该把它们放在一些静态函数。类似于

class Crm_Person extends Crm_CustomerEntity 
{ 
    public static function getData() 
    { 
     $data = Crm_Entity::getData() 
     $newData = (...) 
     return array_merge($data, $newData); 
    } 
} 
1

父类的公共或受保护的变量应该可以由继承它的子元访问,私有变量不可访问。如果房产是公共或私人的,而你仍然无法抵达,更多信息/实际的代码是必需的。

2

你的问题不是很容易理解,但我会检查你的类属性的范围。例如:

class BaseClass { 

    protected $resellerId; 

    public function setResellerId($resellerId) { 
     $this->resellerId = $resellerId; 
    } 
    public function getResellerId() { 
     return $this->resellerId; 
    } 

    // rest of class code here 
} 

class ChildClass extends BaseClass { 
    // class contents 
} 

$obj = new ChildClass; 
$obj->setResellerId(326); 
echo $obj->getResellerId(); // should print '326' to your page 

protected关键字使得该类提供您$resellerId财产,所有子类。此外,公共课程也可用于扩展定义课程的课程,因此我可以在我的ChildClass中使用它们以访问$resellerId资源,该资源也可在我的ChildClass中获得。

0

对于Crm_Person的实例,$ _data数组中没有resellerOrganisationId键值,因为Crm_Entity祖父类中的整个$ _data数组被继承覆盖。你所拥有的就是在Crm_Person类中定义的$ _data数组。

编辑

class Crm_Entity { 

    protected $_mapper = 'Crm_Mapper_Entity'; 
    protected $_data = array(
     'customerId'   => '',  //done 
     'displayName'   => '',  //done 
     'active'    => '',  //done 
     'billingMethod'   => '',  //done 
     'debitorNumber'   => null, //done 
     'bankAccount'   => null, //done 
     'bankAccountTown'  => null, //done 
     'resellerOrganisationId'=> null, 
     'account'    => null, //done 
     'phonenumbers'   => null, //done 
     'calls'     => null, 
     'tickets'    => null, 
     'comments'    => null, 
     'addresses'    => null, 
     'emailaddresses'  => null, //done 
     'actionevents'   => null 
    ); 

} 

class Crm_CustomerEntity extends Crm_Entity { 
    //foobar not done yet 
} 

class Crm_Person extends Crm_CustomerEntity { 

    public function __construct() { 
     $this->_data['customerId'] = null; 
     $this->_data['firstName']  = ''; 
     $this->_data['middleName'] = ''; 
     $this->_data['lastName']  = ''; 
     $this->_data['initials']  = ''; 
     $this->_data['title']   = ''; 
     $this->_data['openingWord'] = ''; 
    } 

    public function getData() { 
     return $this->_data; 
    } 
} 


$test = new Crm_Person(); 
var_dump($test->getdata()); 
+0

嗯,所以我必须array_push在构造函数中的父$ _data的新值? – baklap 2010-08-05 10:32:58

+0

正确。 PHP不会自动合并在继承中碰巧具有相同属性名称的数组属性。你必须通过代码 – 2010-08-05 10:35:23

+0

做到这一点啊超,谢谢你的帮助! – baklap 2010-08-05 10:37:50