$object = new stdClass();
$object->property = 'Here we go';
var_dump($object);
/*
outputs:
object(stdClass)#2 (1) {
["property"]=>
string(10) "Here we go"
}
*/
//object init
$object = (object) [
'propertyOne' => 'foo',
'propertyTwo' => 42,
];
$o= new stdClass();
$o->a = 'new object';
OR
$o = (object) ['a' => 'new object'];
By far the easiest and correct way to instantiate an empty generic php object that you can then modify for whatever purpose you choose:
<?php $genericObject = new stdClass(); ?>
I had the most difficult time finding this, hopefully it will help someone else!
In a variable:
$variable = (object) "lol";
<?php
class Bike
{
function model()
{
$model_name = 'cd100';
echo "bike model:$model_name";
}
}
$obj = new Bike();
$obj->model();
?>
//define a class
class MyClass{
//create properties, constructor or methods
}
//create object using "new" keyword
$object = new MyClass();
//or wihtout parenthesis
$object = new MyClass;
<?php
$fruitsArray = array(
"apple" => 20,
"banana" => 30
);
echo $fruitsArray['banana'];