Questions tagged [stdclass]

`stdClass` is php's generic empty class, kind of like Object in Java or object in Python.But not actually used as universal base class. Useful for anonymous objects, dynamic properties. It is used when casting other types to objects.

stdClass is php's generic empty class, kind of like Object in Java or object in Python.But not actually used as universal base class. Useful for anonymous objects, dynamic properties. It is used when casting other types to objects.

490 questions
1105
votes
17 answers

What is stdClass in PHP?

Please define what stdClass is.
Keira Nighly
  • 14,596
  • 8
  • 27
  • 26
133
votes
7 answers

PHP: Count a stdClass object

I have a stdClass object created from json_decode that won't return the right number when I run the count($obj) function. The object has 30 properties, but the return on the count() function is say 1. Any ideas? Below is an example of one of the…
hellopat
  • 1,609
  • 2
  • 13
  • 14
110
votes
8 answers

How to convert an array into an object using stdClass()

I have made the following array: $clasa = array( 'e1' => array('nume' => 'Nitu', 'prenume' => 'Andrei', 'sex' => 'm', 'varsta' => 23), 'e2' => array('nume' => 'Nae', 'prenume' => 'Ionel', 'sex' => 'm', 'varsta' => 27), 'e3'…
Alexandrw
  • 1,169
  • 2
  • 7
  • 9
93
votes
11 answers

Convert/cast an stdClass object to another class

I'm using a third party storage system that only returns me stdClass objects no matter what I feed in for some obscure reason. So I'm curious to know if there is a way to cast/convert an stdClass object into a full fledged object of a given type.…
The Mighty Rubber Duck
  • 3,838
  • 4
  • 26
  • 26
70
votes
7 answers

When should I use stdClass and when should I use an array in php oo code?

In the middle of a period of big refactorings at work, I wish to introduce stdClass ***** as a way to return data from functions and I'm trying to find non-subjectives arguments to support my decision. Are there any situations when would it be best…
JonG
  • 819
  • 1
  • 6
  • 10
46
votes
5 answers

How to access a property of an object (stdClass Object) member/element of an array?

Doing print_r() on my array I get the following: Array ( [0] => stdClass Object ( [id] => 25 [time] => 2014-01-16 16:35:17 [fname] => 4 [text] => 5 [url] => 6 …
Alex
  • 1,050
  • 1
  • 16
  • 34
43
votes
16 answers

PHP - recursive Array to Object?

Is there a way to convert a multidimensional array to a stdClass object in PHP? Casting as (object) doesn't seem to work recursively. json_decode(json_encode($array)) produces the result I'm looking for, but there has to be a better way...
Peter
  • 3,614
  • 5
  • 33
  • 56
43
votes
7 answers

Object of class stdClass could not be converted to string

I am having a problem with PHP at the moment, I am getting this error, Object of class stdClass could not be converted to string the error occurs when I run this portion of code in my site, function myaccount() { $data['user_data'] =…
sea_1987
  • 2,722
  • 12
  • 41
  • 68
42
votes
3 answers

How to set attributes for stdClass object at the time object creation

I want to set attribute for a stdClass object in a single statement. I don't have any idea about it. I know the following things $obj = new stdClass; $obj->attr = 'loremipsum'; It takes two statements. $obj = (object)…
Mohammed H
  • 6,326
  • 12
  • 72
  • 119
34
votes
4 answers

PHP Foreach Arrays and objects

I have an array of objects; running print_r() returns the output below; Array ( [0] => stdClass Object ( [sm_id] => 1 [c_id] => 1 ) [1] => stdClass Object ( [sm_id] => 1 …
AttikAttak
  • 767
  • 2
  • 12
  • 20
24
votes
9 answers

How to use implode a column from an array of stdClass objects?

I have an array of stdClass objects and I want to build a comma separated list using one specific field of all those stdClass objects. My array looks like this: $obj1 = stdClass Object ( [foo] => 4 [bar] => 8 [foo-bar] => 15 ); $obj2 = stdClass…
ubiquibacon
  • 9,834
  • 25
  • 101
  • 175
23
votes
1 answer

Setting namespace for stdClass object in PHP

Would anyone happen to know if it's possible to set a namespace on a user-defined stdClass object. What I would like to do is to return all the private properties of a class as an object in the form of a getter method. I found one possible solution…
Skittles
  • 2,728
  • 9
  • 27
  • 34
21
votes
2 answers

how do I append an stdClass Object

I have an stdClass Object like generated by joomla like this $db->setQuery($sql); $schoollist = $db->loadObjectList(); And the $schoollist variable contains the following stdClass Objects stdClass Object ( [id] => 1 [col1] => blabla [col2] => 5…
themhz
  • 7,841
  • 19
  • 76
  • 106
20
votes
5 answers

iterating through a stdClass object in PHP

I have an object like this: stdClass Object ( [_count] => 10 [_start] => 0 [_total] => 37 [values] => Array ( [0] => stdClass Object ( [_key] => 50180 …
Mordechai
  • 618
  • 1
  • 5
  • 21
20
votes
4 answers

Add method in an std object in php

Is it possible to add a method/function in this way, like $arr = array( "nid"=> 20, "title" => "Something", "value" => "Something else", "my_method" => function($arg){....} ); or maybe like this $node = (object)…
The Alpha
  • 131,979
  • 25
  • 271
  • 286
1
2 3
32 33