views:

30

answers:

4

Hi!

I try to load an entity with some details and there is a resellerId in the entity.

Now I inherit a subclass from it, and try to access the resellerId but it's not there. How to pass the attributes to the subclasses? I really need it loaded.

Thanks!

edit:

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'   => ''
    );
}

So I need to get the resellerId passed on to the subclass.

+1  A: 

Public or protected variable from the parent class should be accessable by children inheriting from it, private variables are not reachable. If the property is public or private and you still can not reach it, more info/ the actual code is required.

Wrikken
+2  A: 

Your question isn't really easy to understand, but I'd check the scope of your class properties. For example:

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

The protected keyword makes your $resellerId property available in that class, and all sub-classes. Also, public classes become available in classes extending the class in which they're defined, so I can use them in my ChildClass to access the $resellerId property, which is also available in my ChildClass.

Martin Bean
A: 

There is no resellerOrganisationId keyed value in the $_data array for an instance of Crm_Person because the entire $_data array in the Crm_Entity grand-parent class is overridden by the inheritance. All you have is the $_data array as defined in the Crm_Person class.

EDIT

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());
Mark Baker
Hmm so I have to array_push the new values to the parent $_data in the constructor?
baklap
Correct. PHP doesn't automatically merge array attributes that happen to have the same attribute name across an inheritance. You have to do that through code
Mark Baker
Ah super, thank you for your help!
baklap
+2  A: 

Ok, this is clear now.

You're storing data in associative arry. AND you're redefining this array in subclass. Of course, it won't have values form superclass that way.

Move array definition to class constructor instead. Then you should have

<?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);
    }
}

Of course the actual design depends - if you want those mappings before you create class you should put them in some static function instead. Something like

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