PHP libxml_get_external_entity_loader() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
libxml

Definition

In PHP 8.2 and later, libxml_get_external_entity_loader() returns the custom function libxml uses to load external entities. If no custom loader is set, it returns null.

It is the companion of libxml_set_external_entity_loader(). It lets you check or save the current loader so you can restore it later.

Syntax

Call libxml_get_external_entity_loader() with the values you need. Start with this line.

PHP
$loader = libxml_get_external_entity_loader();
CallWhat it does
libxml_get_external_entity_loader()Returns null when libxml is using its default loader.
libxml_set_external_entity_loader($fn); libxml_get_external_entity_loader()Returns the callable you just set.
is_callable(libxml_get_external_entity_loader())A quick check to see if a custom loader is active.

Available as of PHP 8.2.0. On older versions the function does not exist, so check with function_exists() if your code must run on PHP 8.1 or earlier.

Library declarations
PHP
libxml_get_external_entity_loader(): ?callable

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
?callableThe current external entity loader callable, or null if the default loader is in use.

PHP Examples

Example 1 — Check the Default Loader

With nothing configured, libxml uses its built-in loader and the function returns null.

PHP
<?php
var_dump(libxml_get_external_entity_loader());

How It Works

  1. No custom loader has been registered in this script.
  2. libxml_get_external_entity_loader() therefore has nothing to return.
  3. var_dump() prints NULL.
  4. This is the default state on PHP 8. 2 and later.

Example 2 — Read Back a Custom Loader

After registering a loader, the getter returns the same callable.

PHP
<?php
libxml_set_external_entity_loader(function ($public, $system, $context) {
    return null;
});

$loader = libxml_get_external_entity_loader();

echo is_callable($loader) ? "Custom loader is set" : "No loader", PHP_EOL;
echo get_class($loader), PHP_EOL;

How It Works

  1. libxml_set_external_entity_loader() registers a closure that blocks every request.
  2. libxml_get_external_entity_loader() returns that closure.
  3. is_callable() confirms we can call it like a function.
  4. get_class() shows the loader is a Closure object.

Example 3 — Save and Restore the Loader

Save the old loader, use a temporary one, then put the old one back.

PHP
<?php
$previous = libxml_get_external_entity_loader();

libxml_set_external_entity_loader(fn($public, $system, $context) => null);
echo is_callable(libxml_get_external_entity_loader()) ? "Temporary loader active" : "Default", PHP_EOL;

libxml_set_external_entity_loader($previous);
var_dump(libxml_get_external_entity_loader());

How It Works

  1. The first line stores the current loader, which is null here.
  2. A temporary arrow function loader is registered, so the getter returns a callable.
  3. Passing the saved value back to libxml_set_external_entity_loader() restores the old state.
  4. var_dump() shows NULL again because the default loader is back.

Frequently Asked Questions

PHP 8.2.0. It does not exist in earlier versions, so use function_exists() if you support older PHP.
It returns null because libxml uses its built-in loader when no custom callable has been registered.
So you can save it before setting a temporary loader and restore it afterwards. This avoids changing global behavior for the rest of your script.
They are both about external entities, but libxml_disable_entity_loader() is deprecated in PHP 8.0. The set and get pair is the modern way to control loading in PHP 8.2+.
It is whatever callable you passed to libxml_set_external_entity_loader(), such as a Closure, a function name string, or an array callable.

Next: libxml_get_last_error()

libxml_get_last_error() returns the most recent libxml error as a LibXMLError object. If t.

libxml_get_last_error() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful