convert object toArray public function toArray(): array { $properties = get_object_vars($this); $output = []; foreach ($properties as $name => $value) { $output[ucfirst($name)] = $value; } return $output; } PHP7.1 $greet = $foo['bar'] ?? 'hello world';