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

  • Home
  • SEARCH
  • 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 6025563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:19:07+00:00 2026-05-23T04:19:07+00:00

Can you explain the next interesting behaviour? class test { //Class *test* has two

  • 0

Can you explain the next interesting behaviour?

class test {
  //Class *test* has two properties, public and private.
  public $xpublic = 'x1';
  private $xprivate = 'x2';
}
$testObj = new test();

Let’s convert $testObj to array.

settype($testObj, 'array');
var_dump($testObj);

Result:

array(2) {
  ["xpublic"]=> string(3) "x1"
  ["testxprivate"]=> string(4) "x2"
}

OK, xprivate property becomes testxprivate

Let’s convert this array to object.

$newObj = (object)$testObj;
var_dump($newObj);

Result:

object(stdClass)#1 (2) {
  ["xpublic"]=> string(3) "xxx"
  ["xprivate":"test":private]=> string(4) "xxx3"
}

$newObj is a stdClass object.

And the question is:

Why does testxprivate become a private property xprivate (not testxprivate) of the new object? How does PHP know that $testObj array was an object?

If I define the equal array:

$testArray = array('xpublic'=>'x1', 'testxprivate'=>'x2');

and then convert it to object:

var_dump((object)$testArray);

I’ll get the object with two public properties xpublic and testxprivate as expected:

object(stdClass)#2 (2) {
  ["xpublic"]=> string(2) "x1"
  ["testxprivate"]=> string(2) "x2"
}
  • 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-23T04:19:08+00:00Added an answer on May 23, 2026 at 4:19 am

    The array key contains a marker that this should be a private property of the class test.

    Compare your scripts output with the following:

    $array = array(
        "xpublic" => "x1", 
        # this will become a private member:
        "\x00test\x00xprivate" => "x2",
        # this will become a protected member:
        "\x00*\x00xprotected" => "x3"
    );
    
    var_dump($array);
    
    $obj = (object) $array;
    
    var_dump($obj);
    

    When serialized, the same string is used to describe the private members.

    Output:

    array(3) {
      ["xpublic"]=>
      string(2) "x1"
      ["testxprivate"]=>
      string(2) "x2"
      ["*xprotected"]=>
      string(2) "x3"
    }
    
    object(stdClass)#1 (3) {
      ["xpublic"]=>
      string(2) "x1"
      ["xprivate":"test":private]=>
      string(2) "x2"
      ["xprotected":protected]=>
      string(2) "x3"
    }
    

    In the output of var_dump(), the null bytes are not visible.

    (Update: Added protected class member)

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

Sidebar

Related Questions

Can someone explain this result to me. The first test succeeds but the second
Can anyone explain the following behaviour to a relative newbie... const char cInputFilenameAndPath[] =
I hope I can explain this clearly enough. I have my main form (A)
I'm new to WPF and data binding so hopefully I can explain the problem
I've run into a really strange bug, that I'm hoping someone can explain. I
Can someone explain the mechanics of a jump table and why is would be
Can someone explain to me the advantages of using an IOC container over simply
Can you explain STA and MTA in your own words? Also, what are apartment
Can someone explain what exactly the string 0 but true means in Perl? As
Can someone explain what are the benefits of using the @import syntax comparing to

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.