Yii Framework Forum: Two objects contain each other. Is it OK? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Two objects contain each other. Is it OK?

#1 User is offline   phantomlord 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 25
  • Joined: 15-October 17

Posted 09 April 2018 - 02:05 AM

Hello. Is it good practice to have two objects (classes) which contain each other as class properties?
Say:

class Country
{
    public $capital;
}

class City
{
    public $name;
    public $country;
}

$uk = new Country();
$london = new City();
$london->country = $uk;
$uk->capital = $london;




The question is theoretical and isn't related to any framework. Technically it seems OK because in memory there will be just pointers to respective objects. Is it good practice or should I use another apporach?
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,224
  • Joined: 17-January 09
  • Location:Russia

Posted 19 April 2018 - 12:46 PM

It's OK but take into account that it would create circular references. Till both objects are unset memory won't be freed.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users