reference appendices
Magic Constants
Magic constants change value according to where PHP evaluates them. They are useful for diagnostics and path handling, but they should not replace explicit application configuration.
Use This Reference When
- Building paths relative to the current file with
__DIR__. - Logging source context with
__FILE__,__LINE__, or__METHOD__. - Reading code that depends on namespace or trait context.
Inspect Context
PHP example
<?php
echo basename(__FILE__) . PHP_EOL;
echo __DIR__ . PHP_EOL;
// Prints the current filename and directory.
Common values include __LINE__, __FILE__, __DIR__, __FUNCTION__, __CLASS__, __TRAIT__, __METHOD__, and __NAMESPACE__.
Practice
Build a Relative Path
Use __DIR__ to build a path to a sibling config directory. Explain why it is safer than assuming the current working directory.
Show solution
Use $path = __DIR__ . "/config";. The current working directory can change according to the entry point or process manager; the source-file directory is stable.