Library contents

PHP class_exists

A billing application can check whether its configured exporter class is available before choosing a format.

A simple example

class_exists

  1. Start with An optional component is absent
  2. Do this Try to look it up
  3. Result Unavailable
Detecting the missing component lets the program choose a built-in alternative.

Syntax

PHP syntax
class_exists($exporter)
Run these examples in PHP

Save one example at a time as lesson.php. Use PHP 8.2 or later.

php lesson.php

Load an optional invoice exporter

A billing application supports an optional exporter selected by class name. The workstation has only the standard installation, so the app should offer its built-in export format if the extra class is missing.

From the program$exporter = 'BbminfoOptionalInvoiceExporter'
ResultUse the standard export
lesson.php
<?php
$exporter = 'BbminfoOptionalInvoiceExporter';
if (!class_exists($exporter)) {
    echo 'Use the standard export';
}
Output
Use the standard export

The standalone example has no optional invoice exporter installed, so the standard format is available as a fallback.

Advertisements

Load the standard calendar component

The timetable editor is configured with the standard Java date class. Try to load the configured component. A missing optional class selects the fallback.

Givenjava.time.LocalDate
Worked resultComponent available

Handle an unavailable print adapter

A workstation lacks a school-specific optional adapter named in its settings. Try to load the configured component. A missing optional class selects the fallback.

Givenschool.example.OptionalPrinter
Worked resultUse fallback

What to watch for

class_exists uses autoloading by default. A genuine failure inside an autoloader needs its own handling.

Documentation for class_exists