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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:35:02+00:00 2026-06-14T08:35:02+00:00

Sometimes I use __get or stdClass to convert arrays to object . But I

  • 0

Sometimes I use __get or stdClass to convert arrays to object. But I can’t decide I should stick to. I wonder which one is better and faster, any ideas?

class property 
{

    public function __get($name)
    {
        return (isset($this->$name)) ? $this->$name : null;
    }
}

$object = new property();
$object = new stdClass();

so if I use new property(), I will have a property object output,

property Object
(
....
)

while if I use new stdClass(), I will have a stdClass object output,

stdClass Object
(
....
)

so I can get the object data like this $item->title.

EDIT:

how I do the actual array to object conversion.

public function array_to_object($array = array(), $property_overloading = false)
    {

        # If $array is not an array, let's make it array with one value of former $array.
        if (!is_array($array)) return $array;

        # Use property overloading to handle inaccessible properties, if overloading is set to be true.
        # Else use std object.
        if($property_overloading === true) $object = new property();
            else $object = new stdClass();

        foreach($array as $key => $value)
        {
            $key = (string) $key ;
            $object->$key = is_array($value) ? self::array_to_object($value, $property_overloading) : $value;
        }

        return $object;
    }
  • 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-14T08:35:04+00:00Added an answer on June 14, 2026 at 8:35 am

    First off, an (almost) empty class definition like you have is almost like an stdClass so there won’t be any major issues by using either.

    That said, one advantage your “named” class has over stdClass is that you can define what happens when a non-existent property is being accessed by taking advantage of the __get magic method.

    class property
    {
       public function __get($name)
       {
           return null;
       }
    }
    

    The above is a simpler rewrite of your original property class; when __get() is being called, you already know that $this->$name is not set. Although this won’t cause a notice, it doesn’t prevent a fatal error when you try to reference $obj->bla->bla where $obj->bla doesn’t exist.

    It might be more useful to throw an exception when a non-existent property is being accessed:

    class property
    {
       public function __get($name)
       {
           throw new Exception("Property $name is not defined");
       }
    }
    

    This allows your code to catch the exception before it turns into a fatal runtime error that stops your script altogether.

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

Sidebar

Related Questions

I normally use gVim for editing, but I sometimes use vim when remotely connecting
Why sometimes I should use block and other times &block inside functions that accept
I have not had much experience with VBA, but I sometimes use it at
My extension use content-scripts to grab data from some pages. But, sometimes users use
I use simple_html_dom to get site's images. But sometimes, the image's link are not
I use NHiberante at my win service. Sometimes I get System.ObjectDisposedException: Session is closed!
In my application I use AsyncTask in oncreate . Sometimes I get Launch timeout
TCP and UDP(sometimes) use a simple checksum to make sure the contents are correct.
sometimes i use debug code to alert something in javascript (for example, matching something
Background: sometimes I use ps aux | grep process_name to figure out some statistics

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.