A NullPage is a type of Page object that represents a Page that wasn’t found.

NullPage is derived from Page, so it is compatible with most code that would operate on a Page. However, a NullPage isn't saveable nor can it store data. ProcessWire returns a NullPage rather than "null" in many functions so that your API code doesn't have to always check that the returned Page is valid before using it for one purpose or another. The goal being to reduce verbosity and runtime errors in your API code. But there will also be instances where you need to make sure the Page is valid before operating on it…

How to identify a NullPage

The easiest way to identify a NullPage returned by an API function is to check it's ID property. If the ID property is 0, then we can assume it's a NullPage. Granted, a regular Page could also have an ID of 0, but such a Page would not be returned by an API function. The other way to identify a NullPage is to check if it is an instance of NullPage. Either method is valid, and both methods are demonstrated below:

<?php
$page = $pages->get("selector");

// the easiest way…
if(!$page->id) {
    // $page is a NullPage
} 

// or, the most accurate way…
if($page instanceof NullPage) {
    // $page is a NullPage
} 

API functions that can return a NullPage

  • $pages->get() returns a NullPage when it can't find what you asked for. 
  • $page->parent returns a NullPage when the Page has no parent (like the homepage).
  • $page->child() returns a NullPage when the Page has no children, or at least none matching an optionally provided selector.

See Also

$page and Page
$pages

Comments

No comments yet. Be the first to post!

Post a Comment

Your e-mail is kept confidential and not included with your comment. Website is optional.