PHP OOP: Should every object contain all the data?

I’m trying to learn PHP OOP “properly”, and I was wondering, should the constructor grab all the information in the database and store it in the object?

To use an example I’m trying to create using PHP OOP, an internet comic website (much like XKCD’s), would have two classes:

  • Catalogue (list and order of all the episodes)
  • Episode (data relating to that particular episode)

So, in this situation, should the Catalogue object get an array of all the episodeIDs (presumably this would be gotten in the constructor)? And if so, does that mean I have to update both the Object’s array and the Database’s array? And if so, what’s the most common way of implementing this (presumably very common) action?

0

There are a few different approaches you can use to do this, depending on the data set, performance needs, and your general preference.

If there’s an object that is used almost exclusively for display, you would probably want to query the dataset beforehand in some sort of Factory and then create the object by passing the data into the constructor.

If there’s an object that touches a bunch of data sources and is used for different things, loading all of the data at instantiation might take a long time, and all of the object’s data might not be needed every time that object is instantiated. You can leverage lazy loading to accomplish this goal.

If performance is a major concern, and the object stores a ton of data but is mostly immutable, you can use something like the Flyweight pattern, which allows you to actually treat a single object like a collection.

I highly recommend you try a few different ways of doing things. For a project such as yours, how you choose to instantiate and populate the object will probably matter little, but it will be a good and fun exercise in different object instantiation patterns.

As a final note, much of the knowledge you’re gaining now about OOP can be applied to nearly any language you learn in the future.

I am a huge fan of the way Magento has abstracted their database layers into multiple levels and collection model relationships.

This may not answer the question directly but could open the door to thinking about data base objects a bit differently.

The way you would call a single customer is something like:

 $customer = Mage::getModel('customer')->load('20');

Where the getModel method instantiates a class that is magically loaded from an XML definition file. In this case let’s say that it is called Mage_Core_Model_Customer.

Your basic model almost always extends from a parent class which handles database abstraction and that class extends from a generic object class which sets up magic getters and setters for all model classes. So if anything extends from the database abstraction it also picks up the getter and setter methods.

In doing load('20') you are specifying that you would like to load the database object with the key id being 20.

Now specifically on your question. Magento also uses relational mapping between each entity and their seed object. For example a customer can have many addresses. Their address is not naturally returned back as a part of the object, however if you wanted to get a particular attribute not included in the seed table you would do something like this:

$collection = $customer->getCollection()
    ->addAttributeToSelect('address1')
    ->addAttributeToSelect('address2');
$collection->load();

A collection is basically just a list of database objects.

This becomes a well designed solution because the objects are not coupled to each other as a single entity. Instead, you can filter each association separately.

Please no comments about how none of the code here would work in Magento. This was for illustration

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật