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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T07:44:12+00:00 2026-06-09T07:44:12+00:00

How to convert (cast) Object to Array without Class Name prefix in PHP? class

  • 0

How to convert (cast) Object to Array without Class Name prefix in PHP?

class Teste{

    private $a;
    private $b;

    function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

}

var_dump((array)(new Teste('foo','bar')));

Result:

array
  '�Teste�a' => string 'foo' (length=3)
  '�Teste�b' => string 'bar' (length=3)

Expected:

array (
a => 'foo'
b => 'bar' )
  • 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-09T07:44:14+00:00Added an answer on June 9, 2026 at 7:44 am

    From the manual:

    If an object is converted to an array, the result is an array whose elements are the object’s properties. The keys are the member variable names, with a few notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a ‘*’ prepended to the variable name. These prepended values have null bytes on either side. This can result in some unexpected behaviour:

    You can therefore work around the issue like this:

    $temp = (array)(new Teste('foo','bar'));
    $array = array();
    foreach ($temp as $k => $v) {
      $k = preg_match('/^\x00(?:.*?)\x00(.+)/', $k, $matches) ? $matches[1] : $k;
      $array[$k] = $v;
    }
    var_dump($array);
    

    It does seem odd that there is no way to control/disable this behaviour, since there is no risk of collisions.

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

Sidebar

Related Questions

I have vehicle data object returning string array .I want to convert or cast
I have a class Something that implements ISomething . How can I convert/cast from
How would you to convert or cast a List<T> to EntityCollection<T> ? Sometimes this
there are many ways to cast/convert object to another by what the difference between
I use the following code to convert an Object array to a String array
How to convert or make a cast of a List object typized as a
I have some code like this: public class EffectValues : IEnumerable<object> { public object
Is it possible in C++ to convert an array of chars to an object
Given this class with an implicit cast operator: public class MyDateTime { public static
it will give me error Unable to cast object of type . public class

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.