Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 152115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T09:33:56+00:00 2026-05-11T09:33:56+00:00

I’m relatively new to Object Oriented Programming. I pretty much understand the concepts, but

  • 0

I’m relatively new to Object Oriented Programming. I pretty much understand the concepts, but practically speaking, I am having a really hard time finding information about how to best use Models in my Zend Framework applications.

Specifically, I have a Model (that doesn’t extend anything) that doesn’t use a Database Table. It uses getters and setters to access its protected members. I find myself struggling with how to best display this model in the view. I don’t want logic in my view templates, but I find myself in the following situation:

In my controller:

$object = new Object(); $object->setName('Foo Bar'); $this->view->object = $object; 

In my view template:

<h2><?= $this->object->getName() ?></h2> 

I don’t really like calling functions in my view templates but I don’t know a better way to do this. I don’t want my Model’s members to be public, but I basically want to achieve the same results:

<h2><?= $this->object->name ?></h2> 

I don’t want my controller to do all the work of having to know everything about the model:

$object = new Object(); $object->setName('Foo Bar'); $this->view->object = $object; $this->view->object->name = $object->getName(); 

What is the best practice of using models in the Zend Framework? Can anyone recommend any tutorial that would help me understand this Model/View dilemma in Zend Framework?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T09:33:57+00:00Added an answer on May 11, 2026 at 9:33 am

    One possibility is to use the magic __set and __get methods in PHP. I use them like so within my abstract Model class:

    abstract class Model_Abstract {     protected $_data;      // Private Data Members assigned to protected $_data     public function __construct($data = null)     {         // Makes it so that I can pass in an associative array as well as          // an StdObject.         if(!is_object($data)) {             $data = (object) $data;         }          $this->_data = $data;      }      public function __get($key)     {         if (method_exists($this, '_get' . ucfirst($key))) {             $method = '_get' . ucfirst($key);             return $this->$method();                     }         else {             return $this->_data->$key;         }        }      public function __set($key, $val)     {         if ( method_exists( $this, '_set' . ucfirst($key) ) ) {             $method = '_set' . ucfirst($key);             return $this->$method($val);                     }         else {             $this->_data->$key = $val;             return $this->_data->$key;         }     } }   class Model_User extends Model_Abstract {     //Example overriding method for the property firstName in the $_data collection.     protected function _getFirstName()     {         // Do some special processing and then output the first name.     } } 

    This makes it so that you can specify getters and setters for properties as necessary but makes it so that you don’t have to define boilerplate functions for every property, just the ones where you want to do some sort of processing on it before returning the value. For example I use the functionality in a number of places to change ISO compliant dates (as stored in MySQL) into a more compact and readable format for users.

    As far as what to place in your controller, I would recommend looking at this post for some specific feedback on what handling to place within your controller.

    Some feel that they would rather have a helper that automatically loads models into the view and skirts the controller altogether. Personally I would say that within the context of Zend Framework and PHP it makes plenty of sense to pass models into the view from the controller because the state of the models in the view frequently depends on what came from the request (which should definitely be handled in the controller).

    Update: As per criticisms in the comments, one thing that I would point out is that your database access layer and domain (or model) layer are really two different things, though with the Active Record they are blended together. I asked this question a while back and received some useful feedback on this matter. Whatever you decide to do with the model, you’ll want to provide a consistent API for all domain objects regardless of where the data for the model comes from.

    I suppose that one benefit offered by Saem’s answer is that it offers the ability to directly map properties / function return values from one or more domain objects to the view object. Theoretically the usage within the view then looks like this:

    // Mapped from Model_User::_data->last_name and Model_User::_data->first_name $this->name  
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 118k
  • Answers 118k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer As a follow up to Pesto would be nicer to… May 11, 2026 at 11:32 pm
  • Editorial Team
    Editorial Team added an answer Any variable or method that is declared static can be… May 11, 2026 at 11:32 pm
  • Editorial Team
    Editorial Team added an answer How about this? Works for me on IE6, IE7, Firefox.… May 11, 2026 at 11:32 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.