Object Initialization
To create a new object, use the new
statement to instantiate a class:
For a full discussion, see the Classes and Objects chapter.
Converting to object
If an object is
converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass
built-in class is created. If the value was null
, the new instance will be empty. An array converts to an object with properties named by keys and corresponding values. Note that in this case before PHP 7.2.0 numeric keys have been inaccessible unless iterated.
For any other value, a member variable named scalar
will contain the value.
helpful at stranger dot com ¶
10 years ago
By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose:
I had the most difficult time finding this, hopefully it will help someone else!
Anthony ¶
6 years ago
In PHP 7 there are a few ways to create an empty object:
$obj1 and $obj3 are the same type, but $obj1 !== $obj3. Also, all three will json_encode[] to a simple JS object {}:
Outputs: [{},{},{}]
twitter/matt2000 ¶
7 years ago
As of PHP 5.4, we can create stdClass objects with some properties and values using the more beautiful form:
Ashley Dambra ¶
8 years ago
Here a new updated version of 'stdObject' class. It's very useful when extends to controller on MVC design pattern, user can create it's own class.
Hope it help you.
fails and displays:
A
B
Notice: Undefined property: stdClass::$0 in...
mortoray at ecircle-ag dot com ¶
17 years ago
If you use new to create items in an array, you may not get the results you want since the parameters to array will be copies of the original and not references.
By Example:
class Store {
var $item = 3;
}
$a = array[ new Store[] ];
$b = $a;
$a[0]->item = 2;
print[ "|" . $b[0]->item . "|
" ]; //shows 3
$a = array[];
$a[] =& new Store[];
$b = $a;
$a[0]->item = 2;
print[ "|" . $b[0]->item . "|
" ]; //shows 2
This is extremely important if you intend on passing arrays of classes to functions and expect them to always use the same object instance!
Note: The following syntax is desired [or maybe even the default notation should translate as this]:
$a = array[ &new Store[] ];
qeremy [atta] gmail [dotta] com ¶
10 years ago
Do you remember some JavaScript implementations?
// var timestamp = [new Date].getTime[];
Now it's possible with PHP 5.4.*;
or
This will output "bar", and do notice I call on ->$var and not just ->var.
wyattstorch42 at outlook dot com ¶
8 years ago
If you call var_export[] on an instance of stdClass, it attempts to export it using ::__set_state[], which, for some reason, is not implemented in stdClass.
However, casting an associative array to an object usually produces the same effect [at least, it does in my case]. So I wrote an improved_var_export[] function to convert instances of stdClass to [object] array [] calls. If you choose to export objects of any other class, I'd advise you to implement ::__set_state[].
Note: This function spits out a single line of code, which is useful to save in a cache file to include/eval. It isn't formatted for readability. If you want to print a readable version for debugging purposes, then I would suggest print_r[] or var_dump[].
uchephilz ¶
2 years ago
**Shorthand**
`$object = json_decode[json_encode[$array]];`
Or
`$jsonString = json_encode[$array];
$object = json_decode[$jsonString];`
iblun at gmx dot net ¶
17 years ago
To sort an array, that contains an object, after one fieldname inside the object, im using this function:
function objectSort[$objectarray, $field]
{
for [$a=0;$a < [count[$objectarray]]; $a++]
{
for [$b=0;$b < [count[$objectarray]]; $b++]
{
if [$objectarray[$a]->$field < $objectarray[$b]->$field]
{
$temp = $objectarray[$a];
$objectarray[$a] = $objectarray[$b];
$objectarray[$b] = $temp;
}
}
}
return $objectarray;
}
Cosmitar: mhherrera31 at hotmail ¶
11 years ago
i would like to share a curious behavior on casted objects. Casting an object from a class with private/protected attributes results a stdClass with a private/protected attribute for get.
Example:
2. Serialize an object Bug, manipulate the resulting string so that it has BugDetails inside and unserialize it.
See here: //blog.adaniels.nl/articles/a-dark-corner-of-php-class-casting/
Spent two hours looking for more elegant solution, that's my findings.
Trevor Blackbird > yurab.com ¶
16 years ago
You can create a new object using the built-in stdClass or by using type-casting:
Isaac Z. Schlueter i at foohack dot com ¶
14 years ago
In response to Harmor and Mithras, you can use the json functions to convert multi-dimensional arrays to objects very reliably.
Also, note that just using [object]$x doesn't allow you to access properties inline. For example, this is invalid:
However, this function will let you do that, and will also handle multi-dimensional arrays without any hassle.
Note that *numeric* arrays will not be converted to objects using this method.
Ashley Dambra ¶
8 years ago
Class like stdClass but with the possibility to add and execute function.
class stdObject {
public function __construct[array $arguments = array[]] {
if [!empty[$arguments]] {
foreach [$arguments as $property => $argument] {
if [$argument instanceOf Closure] {
$this->{$property} = $argument;
} else {
$this->{$property} = $argument;
}
}
}
}
public function __call[$method, $arguments] {
if [isset[$this->{$method}] && is_callable[$this->{$method}]] {
return call_user_func_array[$this->{$method}, $arguments];
} else {
throw new Exception["Fatal error: Call to undefined method stdObject::{$method}[]"];
}
}
}