Skip to content

Instantly share code, notes, and snippets.

@tommcfarlin
Last active November 4, 2022 00:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommcfarlin/abfec39e0a65e272d7856d88a7ae2fd7 to your computer and use it in GitHub Desktop.
Save tommcfarlin/abfec39e0a65e272d7856d88a7ae2fd7 to your computer and use it in GitHub Desktop.
[PHP] Using Nullable Return Types in PHP
<?php
namespace Acme;
class Post {
// Attributes, Constructor, and Functions here.
}
<?php
namespace Acme;
class PostManager {
private $post;
// Constructor and other Functions here.
public function getPost(): Post
{
return $this->post;
}
}
<?php
namespace Acme;
class PostManager {
private $post;
// Constructor and other Functions here.
public function getPost(): ?Post
{
if ($this->post === null) {
return null;
}
return $this->post;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment