Models as pure data objects in Zend Framework and the service layer
-
06-12-2019 - |
Question
It has become common to treat models in ZF as pure data objects, i.e. a class instance with a bunch of attributes and getters/setters.
class Model_User {
public $id;
public $name;
...
}
What I'm wondering is whether or not it makes sense to, for example, instantiate such an object in a controller and pass it to a service layer, or whether or not the service layer should itself be responsible for instantiating these objects... You would not instantiate a Zend_DbTable
class directly in the controller, so does it make much sense to instantiate a Model_User
in a controller to pass it to a service?
Solution
I don't see a problem with doing it this way. Data Transport Objects (DTO) are meant to pass information between layers and/or modules. In my opinion this is no different that using arrays and it certainly is easier than using arrays.
Here is another discussion over DTOs - What is Data Transfer Object?
And Martin's definition on DTOs - http://martinfowler.com/eaaCatalog/dataTransferObject.html