Thousands of functions and classes are available to a PHP file with no import. All of them come from extensions, which are compiled into PHP or loaded at start-up. This page lists what is built in and the interfaces that connect a class to the language. The manual’s Function Reference documents each one.
Extensions
get_loaded_extensions() and php -m list what is present, and
extension_loaded() tests for one. The manual groups them by
membership.
| Group | Extensions |
|---|---|
| Core: always present, and cannot be left out when PHP is built | Arrays, Classes/Objects, Date/Time, Directories, Error Handling, Program execution, Filesystem, FastCGI Process Manager, Function Handling, Hash, PHP Options/Info, JSON, Mail, Math, Misc., Network, OPcache, Output Control, Password Hashing, PCRE, Random, Reflection, SPL, Streams, Strings, URI, URLs, Variable handling |
| Bundled: shipped with PHP, can be left out | Apache, BC Math, Calendar, COM, Ctype, DBA, Exif, FFI, Fileinfo, Filter, FTP, iconv, GD, intl, LiteSpeed, Multibyte String, Mhash, PCNTL, PDO, Phar, phpdbg, POSIX, Semaphore, Sessions, Shared Memory, Sockets, SQLite3, Tokenizer, Zlib |
Others need an external library at build time, such as cURL, and further extensions are published through PECL.
Predefined interfaces
A class implements these to work with language features.
| Interface | Methods | Lets an object be used with |
|---|---|---|
Traversable | None | foreach. It cannot be implemented directly: use one of the next two |
Iterator | current, key, next, rewind, valid | foreach, by iterating itself |
IteratorAggregate | getIterator(): Traversable | foreach, by returning an iterator. A generator is a common return value |
ArrayAccess | offsetExists, offsetGet, offsetSet, offsetUnset | $obj['key'] |
Countable | count(): int | count($obj) |
Stringable | __toString(): string | String conversion. Implemented automatically by any class with __toString() (PHP 8.0) |
JsonSerializable | jsonSerialize(): mixed | json_encode() |
Throwable | See Exceptions | throw and catch. Extend Exception or Error rather than implement it |
UnitEnum | cases(): array | Added to every enum |
BackedEnum | from, tryFrom | Added to every backed enum |
Serializable | serialize, unserialize | The older serialisation interface. Using it without __serialize() is deprecated since 8.1 |
InternalIterator | None usable | Internal, for extension classes that need an iterator |
The SPL extension adds OuterIterator, RecursiveIterator,
SeekableIterator, SplObserver and SplSubject.
final class Bag implements Countable, IteratorAggregate, ArrayAccess
{
public function __construct(private array $items = []) {}
public function count(): int { return count($this->items); }
public function getIterator(): Generator { yield from $this->items; }
public function offsetExists(mixed $k): bool { return isset($this->items[$k]); }
public function offsetGet(mixed $k): mixed { return $this->items[$k]; }
public function offsetSet(mixed $k, mixed $v): void { $this->items[$k] = $v; }
public function offsetUnset(mixed $k): void { unset($this->items[$k]); }
}
count(), foreach and square bracketsPredefined classes
| Class | Purpose |
|---|---|
stdClass | A generic empty class, the result of casting to an object. It accepts dynamic properties |
Closure | An anonymous function or first-class callable. See Functions |
Generator | Returned by a generator function |
Fiber | A function with its own call stack |
WeakReference | A reference to an object that does not stop it being destroyed (PHP 7.4) |
WeakMap | A map with object keys whose entries vanish when the object does (PHP 8.0) |
Attribute | Marks an attribute class |
SensitiveParameterValue | Stands in for a parameter value hidden in a stack trace |
__PHP_Incomplete_Class | Created by unserialize() for a class that is not loaded |
Directory | Returned by dir() |
php_user_filter | Base class for stream filters written in PHP |
ArrayObject, ArrayIterator | Objects wrapping an array |
DateTimeImmutable, DateTime, DateTimeZone, DateInterval, DatePeriod | Dates and times |
Exception, Error and subclasses | See Exceptions |
Fibers
A Fiber (PHP 8.1) is a function
that can be paused from anywhere in its call stack and resumed later. Unlike a
generator, it has its own stack, so a function deep in the call chain can suspend.
Calling code needs no change. Fibers do not run in parallel: one runs at a time.
$fiber = new Fiber(function (): void {
$value = Fiber::suspend('paused');
echo "Resumed with $value\n";
});
echo $fiber->start(), "\n"; // paused
$fiber->resume('hello'); // Resumed with hello
Standard PHP Library
SPL holds the data structures and iterators that the language does not have as syntax (manual).
| Group | Classes |
|---|---|
| Data structures | SplStack, SplQueue, SplDoublyLinkedList, SplMinHeap, SplMaxHeap, SplPriorityQueue, SplFixedArray, SplObjectStorage |
| Iterators | ArrayIterator, LimitIterator, InfiniteIterator, CallbackFilterIterator, RecursiveArrayIterator, RecursiveIteratorIterator, DirectoryIterator, GlobIterator |
| Files | SplFileInfo, SplFileObject, SplTempFileObject |
| Exceptions | LogicException, RuntimeException and their subclasses |
| Autoloading | spl_autoload_register(); see Autoloading |
Standard functions
Functions are global and have no consistent naming scheme, for historical reasons. The main families:
| Area | Examples |
|---|---|
| Strings | strlen, substr, str_contains, str_starts_with, str_replace, explode, implode, sprintf, trim, htmlspecialchars |
| Arrays | count, array_map, array_filter, array_reduce, array_merge, array_keys, sort, usort, in_array, array_is_list, array_first |
| Maths | abs, floor, round, intdiv, fdiv, random_int, max, min |
| Files and streams | fopen, fread, fclose, file_get_contents, file_put_contents, is_file, unlink |
| Output and buffering | echo, printf, print_r, var_dump, ob_start, header |
| Data formats | json_encode, json_decode, json_validate, serialize, unserialize, base64_encode, pack, unpack |
| Regular expressions | preg_match, preg_replace, preg_split |
| Hashes and passwords | hash, password_hash, password_verify, random_bytes |
| Time | time, date, strtotime, microtime |
| Introspection | function_exists, class_exists, method_exists, get_debug_type, is_callable, and the Reflection classes |
Built-in functions take parameters named in the manual, and accept named arguments since PHP 8.0.