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

  • SEARCH
  • Home
  • 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 7668633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:24:41+00:00 2026-05-31T15:24:41+00:00

Using PHP 5.3, I am currently writing an MVC application and need to extract

  • 0

Using PHP 5.3, I am currently writing an MVC application and need to extract the data that was passed in to a model for validation, to send back to the view for repopuating a form on error.

The fields in the model are marked private and can only be accessed if they appear in a list of fieldNames (so controllers can’t attempt to change the contents of other ‘business’ fields in the class.

In the controller area marked ‘BUG’ below, attempting to access the private field just results in the array item being created but set to null, not the value. While in my debugger, if I inspect the field of the source ($templateM->$field) it shows the correct value.

What’s going on?

In the model base class:

class model {

protected $fieldNames;

public function __get($name)
{
    if ($name === 'fieldNames')
        return $this->fieldNames; // Special exception for getting the list of fields
    if ($name === 'errorList')
        return $this->errorList; // special exception for getting the current errors
    if (in_array($name, (array)$this->fieldNames))
        return $this->$name;
    else
        throw new Exception('Invalid access to private model field');
}
}

In the model:

class template extends model
{
function __construct()
{
    parent::__construct();
    $this->fieldNames = new immutableArray(array('id', 'etc'));
}

private $id = 0;
private $etc = 1;
}

In the controller:

class templateManager extends controller
{
    function create()
    {
        // Validate form data
  $templateM = getModel('template');
  $templates = array();
  $bodyData = array();
  $bodyData['showSuccess'] = false;

  $result = $templateM->validate();

  if ($result)
  {
      if ($templateM->save())
      {
          $bodyData['showSuccess'] = true;
      }
  }

  // Load present data (post insert)
  $templates = $templateM->getAllAsArray();

  $bodyData['errorMessages'] = (array)$templateM->errorList;

  $formData = array();
  if (count($bodyData['errorMessages']) > 0)
  {
      foreach($templateM->fieldNames as $field)
            {
                $formData[$field] = $templateM->$field; // <- BUG
            }
  }

  $bodyData['formData'] = $formData;

  $bodyData['templateData'] = $templates;
  $this->_drawPage($bodyData);
}
  • 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. Editorial Team
    Editorial Team
    2026-05-31T15:24:42+00:00Added an answer on May 31, 2026 at 3:24 pm

    Actually i would recommend for you to stop abusing __get(). You already have too many if’s in it, and that list will get only longer and longer. Better to make proper getter and setter methods.

    AS for the cause of your problems: the Model::__get() cannot access the private variables. It will work if you define them as protected.

    Additionally, you might find this rant useful. At least the “side notes” part of it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently developing a PHP application that's using an Access database as a backend.
I'm writing a PHP application that will store STUDENT data in a MySQL relational
I'm currently using PHP, JAVASCRIPT, MYSQL, XHTML, CSS to develop my site. Note that
I am currently using PHP/MySQL and the jQuery timeago plugin. Basically, I need to
I am currently using various back-end services and I want to use PHP to
I'm using the JQuery AJAX function to deliver data to a PHP doc. Currently,
I am currently writing a CRUD class in PHP using PDO. I like the
I'm currently writing a PHP script that 3rd party clients will be able to
I am currently translating my PHP application using gettext with POEdit. Since I respect
I am writing a PHP function that will need to loop over an array

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.