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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:24:49+00:00 2026-05-16T07:24:49+00:00

I have a parent object that I use for general CRUD in my applications

  • 0

I have a parent object that I use for general CRUD in my applications – it has basic save & retrieve methods so I can don’t have to reinclude them them in all my objects. Most of my child objects extend this base object. This has worked fine, but I’m finding a problem with retrieving a serialized child object. I use a “retrieve” method in the parent object that creates an instance of the child, then populates itself from the properties of the unserialized child – this means is can “self unserialize” the object.

Only problem is – if the child object has a protected or private property, the parent object can’t read it, so it doesn’t get picked up during retrieval.

So I’m looking either for a better way to “self unserialize” or a way to allow a parent object to “see” the protected properties – but only during the retrieval process.

Example of the code:

BaseObject {

 protected $someparentProperty;

 public function retrieve() {

  $serialized = file_get_contents(SOME_FILENAME);
  $temp = unserialize($serialized);
  foreach($temp as $propertyName => $propertyValue) {
    $this->$propertyName = $propertyValue;
  }     

 }

 public function save() {

    file_put_contents(SOME_FILENAME, serialize($this));
 }
}

class ChildObject extends BaseObject {

 private $unretrievableProperty;  

 public setProp($val) {
    $this->unretrivableProperty = $val;
 }
}

$tester = new ChildObject();
$tester->setProp("test");
$tester->save();

$cleanTester = new ChildObject();
$cleanTester->retrieve();
// $cleanTester->unretrievableProperty will not be set

EDITED: Should have said “Private” not protected child properties.

  • 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-16T07:24:50+00:00Added an answer on May 16, 2026 at 7:24 am

    It doesn’t seem that same class visibility policy applies to iherited/parent classes. The php documentation does not address this.

    I would suggest that you declared the retrieve method static, and fetched the $cleanTester through a static call rather than your current “self unserialize” approach.

    static function retrieve() {
      $serialized = file_get_contents(SOME_FILENAME);
      return unserialize($serialized);
    }
    
    [...]
    
    $cleanTester = BaseObject::retrieve();
    

    Or you could utilize the __get() method to access inaccessible properties… I believe this could be added to the BaseObject class and fetch protected properties from the child class. Since the same class visibility policy should apply to BaseObject you could define the __get() method private or protected.

    BaseObject {
      private function __get($propertyName) {
        if(property_exists($this,$propertyName))
          return $this->{$propertyName};
    
        return null;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a parent object called Page that has a List of objects called
I have a Post model object that has reference to a parent object. The
I have a parent object which has a one to many relationship with an
Have 2 tables for example: In 1st: object & parent columns object | parent
I have a parent object (part of a DAL) that contains, amongst other things,
I have a parent object of type Parent and it currently has a null
I have a parent object, Post, which has the following children. has_one :link has_one
I have a parent class called Exam and that has many instances of a
I have a MenuObject class that represents a websites Top Menu. This object has
I have a parent object Compound with a one-to-many relationship to an object Submission.

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.