php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #34500 change stdClass constructor prototype
Submitted: 2005-09-14 13:04 UTC Modified: 2005-09-20 16:14 UTC
From: stochnagara at hotmail dot com Assigned:
Status: Closed Package: Documentation problem
PHP Version: 5CVS-2005-09-14 (snap) OS:
Private report: No CVE-ID: None
 [2005-09-14 13:04 UTC] stochnagara at hotmail dot com
Description:
------------
I think it will be very useful to modify stdClass constructor from __construct() to __construct ($fields = array()).

This will make stdClass easier to work with like arrays.


Reproduce code:
---------------
$array = array ('a' => 2, 'b' => 3);
$obj = new stdClass;
$obj->a = 2;
$obj->b = 3;
//Not supported
$obj = new stdClass (array ('a' => 2, 'b' => 3));



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-09-14 13:06 UTC] stochnagara at hotmail dot com
I forgot: 'a' and 'b' become class members so

echo $obj->a prints 2 and echo $obj->b prints 3.
 [2005-09-14 13:09 UTC] tony2001@php.net
stdClass is a dummy class that doesn't have any methods, including constructor, so there is nothing to change.
Declare and use your class.
 [2005-09-14 14:57 UTC] stochnagara at hotmail dot com
Ok, then I will reformulate my feature request : Function get_object_vars applied to stdClass returns array containing all object properties and their values. 

I'd like to see a function which is the opposite of get_object_vars, e.g. array_to_object ($array) which converts an array to a stdClass object with array keys as properties and array values as proerties values.

Many users will appreciate it. See Drupal too:)
 [2005-09-14 15:07 UTC] tony2001@php.net
Then convert array to object this way:
<?php
$a = array('a'=>'b', 'foo'=> 'bar'); 
var_dump((object)$a);
?>

 [2005-09-14 17:28 UTC] stochnagara at hotmail dot com
So I reopen the bug and change the category to Documentation problem. There is no place in the documentation where this functionality (casting array to object) is described.
Even http://www.php.net/manual/en/language.types.object.php misleads readers. The description says that everything except null is converted to stdClass object with a member named scalar which contains the value of the converted variable.
This is the apropriate place to mention what happens when an array is converted into object.
 [2005-09-20 16:14 UTC] vrana@php.net
This bug has been fixed in the documentation's XML sources. Since the
online and downloadable versions of the documentation need some time
to get updated, we would like to ask you to be a bit patient.

Thank you for the report, and for helping us make our documentation better.

"Array converts to an object with properties named by array keys and with corresponding values."
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 21:01:31 2025 UTC