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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:41:36+00:00 2026-06-08T06:41:36+00:00

This has to be simple, but I can’t seem to find an answer…. I

  • 0

This has to be simple, but I can’t seem to find an answer….

I have a generic stdClass object $foo with no properties. I want to add a new property $bar to it that’s not already defined. If I do this:

$foo = new StdClass();
$foo->bar = '1234';

PHP in strict mode complains.

What is the proper way (outside of the class declaration) to add a property to an already instantiated object?

NOTE: I want the solution to work with the generic PHP object of type stdClass.

A little background on this issue. I’m decoding a json string which is an array of json objects. json_decode() generates an array of StdClass object. I need to manipulate these objects and add a property to each one.

  • 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-08T06:41:37+00:00Added an answer on June 8, 2026 at 6:41 am

    If you absolutely have to add the property to the object, I believe you could cast it as an array, add your property (as a new array key), then cast it back as an object. The only time you run into stdClass objects (I believe) is when you cast an array as an object or when you create a new stdClass object from scratch (and of course when you json_decode() something – silly me for forgetting!).

    Instead of:

    $foo = new StdClass();
    $foo->bar = '1234';
    

    You’d do:

    $foo = array('bar' => '1234');
    $foo = (object)$foo;
    

    Or if you already had an existing stdClass object:

    $foo = (array)$foo;
    $foo['bar'] = '1234';
    $foo = (object)$foo;
    

    Also as a 1 liner:

    $foo = (object) array_merge( (array)$foo, array( 'bar' => '1234' ) );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure this has a very simple answer, but I can't seem to find
I'm sure this question has a simple enough answer, but I can't seem to
okay, this has got to be simple - but I can't seem to find
I'm sure this is quite simple but I can't find the answer. I'm passing
This has a potentially simple answer but I can't figure it out - double
This seems like a simple question, but has proven to be difficult to find
I'm hoping this question has a very simple answer. I can think of ways
I'm sure this has been answered before but I can't find the right nomenclature
I know this has been discussed over and over again but I can't find
This has probably been asked before but I can't find any relevant post using

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.