Hard
What will this code display?
<?php
class Foo
{
private ?string $bar;
public function setBar(?string $secondLine): void
{
$this->bar = $secondLine;
}
public function getBar(): ?string
{
return $this->bar;
}
}
$foo = new Foo();
echo $foo->getBar() ?? 'john doe';
Author: W3D TeamStatus: PublishedQuestion passed 951 times
Edit
6
Community Evaluations
Paul TOTI
06/05/2024
Si $bar n'a jamais été défini, elle est null par défaut en raison de son type nullable ( ?string). L'accÚs à une propriété nullable non initialisée qui est lue (et non écrite ou utilisée dans un contexte nécessitant une valeur non nulle) retourne simplement null. Il n'y a pas d'erreur car PHP gÚre cette situation de maniÚre interne en retournant null
Similar QuestionsMore questions about PHP
4
Is this function signature valid?
```php
<?php
interface Demo
{
public function bar(): ?mixed;
}
```3
Which function signature is valid?3
PHP function with named and positional arguments.3
Attributes are metadata that can be added to classes, properties, constants or methods.3
Is the following code possible in PHP 8?
```php
$name = âmy_argument_nameâ;
$value = 25;
myFunction($name: $value);
```