php class object code example
Example 1: php stdclass
$person=new stdClass();
$person->firstName="Chuck";
$person->lastName="Bartowski";
$person->age=27;
print_r($person);
Example 2: object php
$object = (object) [
'propertyOne' => 'foo',
'propertyTwo' => 42,
];
Example 3: php define class
class Bike {
function Bike() {
$this->type = 'BMX';
}
}
$blackSheep = new Bike();
print $blackSheep->type;
Example 4: create a class in php
<?php
class Fruit {
public $name;
public $color;
function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
function get_name() {
return $this->name;
}
function get_color() {
return $this->color;
}
}
$apple = new Fruit("Apple", "red");
echo $apple->get_name();
echo "<br>";
echo $apple->get_color();
?>
Example 5: php object
$o= new \stdClass();
$o->a = 'new object';
OR
$o = (object) ['a' => 'new object'];
Example 6: php object
$obj = [
"example1" => 1,
"example2" => "two",
"example3" => [
"example4": true
]
]