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 8956703
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:50:36+00:00 2026-06-15T14:50:36+00:00

Can anyone tell me if there is a resolution to saving PHP classes with

  • 0

Can anyone tell me if there is a resolution to saving PHP classes with private members to MongoDb? I keep getting the following error

zero-length keys are not allowed, did you use $ with double quotes?'

I see there at least two existing question pertaining to the same question with no real answers.
Question 1
Question 2

all of my persistence classes have a private member that I need available but I’m not interested in creating a function to avoid the necessity for the private member since this is an instance based class that has multiple function utilizing the private member.

Web server Apache/2.2.22

PHP version PHP 5.4.6

PHP extension mongo/1.2.6

This would be a sample implementation, please don’t critique the code itself it is just to illustrate the type of behavior which is the Save of $this and the $private member in the base type:

<?php

class PersistableObject extends AbstractBasePersistableObject
{
   public  $PublicSubTypeProperty;

    public function __construct()
    {
       parent::__construct();
    }

    public function GetDalConfigurationFromSubType()
    {
        //This object is just a wrapper DAL implemented based on
        //the php Mongo, MongoDb and MongoCollection objects
        return new MongoBasedDal();
    }
}


abstract class AbstractBasePersistableObject
{
   private $dalRef = null;
   public $PublicBaseProperty;

   public abstract function GetDalConfigurationFromSubType();

   public function __construct()
   {
      $this->dalRef = $this->GetDalConfigurationFromSubType();
   }

   public function Save()
   {
       $this->dalRef->Save($this);
   }
}

?>
  • 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-06-15T14:50:36+00:00Added an answer on June 15, 2026 at 2:50 pm

    Can anyone tell me if there is a resolution to saving PHP classes with private members to MongoDb?

    The problem, as you know from Google Groups, is that the PHP driver actually omits private and protected variables from save. This is a fundamental problem within the core of the driver itself so there is no easy way around it. As you also know Derick created a JIRA for this: https://jira.mongodb.org/browse/PHP-624

    @harald does actually state the way around, to create your own _to_array() and feed that into the save function, unfortunately this derives a fundamental problem again with how you have built your classes. You must use reflection for this like so:

    $reflect = new ReflectionClass(get_class($this));
    $class_vars = $reflect->getProperties(ReflectionProperty::IS_PUBLIC | ReflectionProperty::IS_PROTECTED | ReflectionProperty::IS_PRIVATE);
    
    foreach ($class_vars as $prop) {
        $doc[$prop->getName()] = $this->{$prop->getName()};
    }
    

    And then use $doc within the save.

    You can however change your programming a bit to avoid the reflection (or just cache the result of the reflection to save the resource hog) by making a pre-defined array of $schema which will house all the fields you wish to save. When you wish to add new fields on-demand you can just make your __set and __get update the in memory schema array. This way you can avoid the reflection altogether and just go for grabbing the schema array and it’s values.

    This is the method a lot of frameworks use to get around the problem, including Lithium, Yii, Kohona, CakePHP and more.

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

Sidebar

Related Questions

Question 1 Can anyone tell me if there is any difference between following 2
Can anyone tell me if there is a way to select a video from
Can anyone please tell me is there any special requirement to use either EXTERN
Can anyone tell me what is causing the space below the images? There seems
Can anyone tell me why the following code fails to submit the form into
Can anyone tell me if there's a quick way to format your code in
Can anyone tell me if there is a similar module for document management for
can anyone tell me if there is any alternative for visual asssit? We are
Can anyone tell me why this breaks when i put an image tag there?
Can anyone tell me if there's a way to run a thread and give

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.