Current Location: Home> Latest Articles> Check service instances with is_a() in the service locator

Check service instances with is_a() in the service locator

M66 2025-05-30

In PHP development, Service Locator is a commonly used design pattern for managing and obtaining various service instances in applications. To ensure that the acquired service instance meets the expected type, type checking is usually required. The is_a() function is a very practical tool in PHP. It can be used to determine whether an object is an instance of a certain class, or whether it is inherited from that class.

This article will introduce in detail how to use the is_a() function in the service locator to check the type of service instance, and help understand it with code examples.


What is the is_a() function?

is_a() is a built-in PHP function that determines whether an object belongs to a certain class or its subclass. The function definition is as follows:

 bool is_a(object $object, string $class_name, bool $allow_string = false)
  • $object : The object to be checked.

  • $class_name : target class name.

  • $allow_string : If true , $object is also allowed to be a string of the class name.

The function returns true to indicate that the object is an instance of the class or subclass, and false to indicate that it is not.


Check service instances using is_a() in service locator

Suppose you have a service locator class that manages the creation and acquisition of services. You want to make sure that the service instances you get from the locator are of the expected type.

Sample code

 <?php

class ServiceLocator
{
    private $services = [];

    // Register a service factory
    public function set(string $name, callable $factory)
    {
        $this->services[$name] = $factory;
    }

    // Get a service instance
    public function get(string $name)
    {
        if (!isset($this->services[$name])) {
            throw new Exception("Service '$name' not found.");
        }
        return $this->services[$name]();
    }
}

// Define an interface and implementation class
interface LoggerInterface
{
    public function log(string $message);
}

class FileLogger implements LoggerInterface
{
    public function log(string $message)
    {
        echo "Log to file: $message\n";
    }
}

// Create a service locator instance
$locator = new ServiceLocator();

// register FileLogger Serve
$locator->set('logger', function () {
    return new FileLogger();
});

// 获取Serve并检查type
$logger = $locator->get('logger');

if (is_a($logger, 'LoggerInterface')) {
    $logger->log("This is a legal log instance。");
} else {
    echo "mistake:Serve实例不是 LoggerInterface type。\n";
}

In the above example, we define a ServiceLocator class to store and retrieve services. The is_a() function is used to ensure that the service $logger obtained is an instance that implements LoggerInterface .


How to replace a domain name when using URL example?

If you need to use a URL in your code, such as service configuration or interface request, replace the domain name in the URL with m66.net as required. For example:

 $url = "https://m66.net/api/v1/service";

This allows for easy unified management and testing.


summary

  • The is_a() function is a concise tool for judging object types in PHP.

  • In the service locator mode, is_a() can be used to ensure that the returned service instance complies with the expected interface or class.

  • Unified URL domain names can facilitate the management of service configuration and are more flexible during testing.

Through this article, you can manage service instance types more securely in your project to avoid potential problems caused by type mismatch.