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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T23:36:04+00:00 2026-05-16T23:36:04+00:00

This may be impossible, but I thought I’d ask before I rework the whole

  • 0

This may be impossible, but I thought I’d ask before I rework the whole thing…:

I have an object class that can receive “pieces”, which are also objects. It works basically like this:

class myObject {
    //Receives an associative array of "Piece" objects
    function __construct($objects) {
        foreach( $objects as $k=>$v ) {
            $this->{$k} = $v;
        }
    }
}

I’m omitting a lot of code obviously, but I hope this gives you the idea. Basically I have a ton of different “piece” objects that do different things, and if I pass an array of them into myObject then myObject becomes a very flexible and useful class for doing all kinds of different things.

So, I could use this to create a Book object and have pieces that included a “Author Piece” and an “ISBN Piece”, and those pieces would know how to validate data etc. So I might have $book with objects set to the member variables author and isbn.

This is great because I can do things like:

echo $book->author; //all Pieces have a __toString() method.
echo $book->author->firstName;
$book->author->showBio();
$book->author->contactForm();

…and so on.

Now to the point. This system works great, and one of the things that makes it great is that I can pick and choose any of these pieces that I like and stick them into an object to bind them together.

But the problem is, I don’t want someone else who might use the code later to try:

$book->author = "John Doe";

…because then they’d just have a value instead of the author object. I’d like that to give them an error and instruct them to do this instead:

$book->author->setName("John Doe");

So because I don’t know in advance what pieces might be in any individual object (and the entire point is to be able to have the freedom to instantly assemble any kind of object), I can’t just set private $author in the class declaration.

I tried fooling around with __get() and __set() a bit, but I couldn’t get it to work without compromising the functionality of the objects as they are now.

So, like I said, I know this may be impossible, but before I give up, I thought I’d ask. Is there a way to protect the property of an object after it has been created without declaring it in the class definition?

  • 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-16T23:36:05+00:00Added an answer on May 16, 2026 at 11:36 pm

    It’s absolutely not impossible. You should overwrite the magic __get and __set functions

    Like this:

    class myObject {
      protected $data = array();
    
      public function __construct($objects) {
        foreach( $objects as $k=>$v ) {
          $this->data[$k] = $v;
        }
      }
    
      /* your code */
    
      public function __get($name) {
        if(array_key_exists($name, $this->data)) {
          return $this->data[$name];
        }
        return null;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may be something I just have to live with, but does the blockUI
This may sound easy, but please, I am a newbie. I have a simple
This may be a n00b topic but, anyways, I have been having a rather
this may be a simple question but am struggling to understand how to solve
This may seem like a simple problem to solve, but I'm new to Android
This may seem trivial, but i'm working on a project to be deployed exclusively
This may be a very basic question but somehow it got me tricked... when
This may be a really simple question, but I'm trying to create the database
this may be a stupid question but I wanted to convert a 2 dimensional
We're often working on a project where we've been handed a large data set

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.