1/43
Looks like no tags are added yet.
Magic Methods
special methods PHP calls automatically when certain events happen to an object; they always start with a double underscore (__).
Automatic
you never call magic methods directly; PHP triggers them for you.
Reserved names
fixed names like __get; you should not invent your own methods.
Powerful
Control creation, property access, printing, cloning and more.
__construct
runs when an object is created; use it to set initial state.
__destruct
runs when the object is destroyed or the script ends; use it to clean up (close files, connections). Not guaranteed to run on fatal errors — don’t rely on it for critical work.
Constructor promotion (PHP 8)
declares and assigns properties in one step inside the constructor.
__get
called when reading an inaccessible (private/undefined) property.
__set
called when writing an inaccessible (private/undefined) property; great for validation and controlled access.
__toString
defines how an object behaves as a string (echo, concatenation); must return a string.
__invoke
lets you call an object like a function; suits callbacks like array_map().
__call
handles calls to undefined or inaccessible instance methods.
__callStatic
does the same as __call but for static methods; useful for dynamic APIs and proxies.
__clone
runs on the new copy after using the clone keyword; use it to make a deep copy so nested objects aren’t shared (a plain clone is shallow — nested objects stay shared).
__isset
runs when _____ or empty() is used on an inaccessible property; without it, _____ on hidden properties returns false.
__unset
runs when ____ is used on an inaccessible property.
Exception
an unexpected event that interrupts normal program flow (e.g. file not found, division by zero, database error).
try
the block where you place code that might cause an error.
catch
handles the error if one happens.
finally
runs always, whether or not an error happened.
DivisionByZeroError
thrown when dividing a number by zero.
TypeError
thrown when passing the wrong data type to a function.
ValueError
thrown when a value has a valid type but an invalid value.
ParseError
thrown for syntax errors in code.
Throwable
the parent of both Exception and Error; catching it catches both.
Use specific exception classes
create different custom exceptions for different errors.
Catch only what you can handle
avoid catching exceptions you can’t properly handle.
Use finally for cleanup
always use finally to close files, DB connections, or show a message.
Avoid silent fails
never catch errors without logging or notifying.
Namespace
like a folder or container for your code; organizes classes, functions, and constants and avoids name conflicts between parts of your program or with external libraries.
Why namespaces are needed
without them, two classes with the same name (e.g. two User classes) in the same project cause PHP to throw an error since it can’t tell which one you mean.
use keyword
imports a namespaced class so you don’t have to write the full path every time; can also be used to give it an alias.
Avoids conflicts (namespace benefit)
prevents name collisions between classes/functions.
Organized structure (namespace benefit)
groups related code logically.
Readable code (namespace benefit)
makes a class’s purpose and location obvious.
Autoloading support (namespace benefit)
works seamlessly with Composer and PSR-4.
Team-friendly (namespace benefit)
reduces merge and naming issues in teams.
Reusable modules (namespace benefit)
lets you reuse components across projects.
Safe package use (namespace benefit)
allows multiple third-party libraries to be used safely together.
Autoloading
PHP automatically loads class files when you use a class, so you don’t need to write require or include for every class manually.
spl_autoload_register()
lets you register a custom function that tells PHP how to find and load class files whenever a class is used.
PSR-4
the official PHP Standard Recommendation (#4) that defines how to automatically load classes from file paths based on their namespaces; published by PHP-FIG. In simple terms: Namespace = Folder, Class Name = File Name.
Composer
a dependency/autoloading tool; you define your namespace mapping in composer.json, then run composer dump-autoload to generate a vendor/ folder and vendor/autoload.php.
composer.json
the file where you declare the root namespace prefix (e.g. “App\”) and the folder it maps to (e.g. “src/App/”).