Php继承超类变量

时间:2010-08-05 09:57:49

标签: php oop inheritance variables subclass

我尝试加载一个包含一些细节的实体,并且实体中有一个resellerId。

现在我从中继承了一个子类,并尝试访问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传递给子类。

4 个答案:

答案 0 :(得分:3)

好的,现在很清楚了。

您将数据存储在关联arry中。你在子类中重新定义了这个数组。当然,它不会以这种方式形成超类的值。

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

<?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中使用}。

答案 2 :(得分:1)

父类中的公共变量或受保护变量应该可以由继承它的子进程访问,私有变量是不可访问的。如果该属性是公共或私人属性,但您仍无法访问该属性,则需要更多信息/实际代码。

答案 3 :(得分: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());