Lizzy McOOP

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/43

encourage image

There's no tags or description

Looks like no tags are added yet.

44 Terms

1
New cards

Magic Methods

special methods PHP calls automatically when certain events happen to an object; they always start with a double underscore (__).

2
New cards

Automatic

you never call magic methods directly; PHP triggers them for you.

3
New cards

Reserved names

fixed names like __get; you should not invent your own methods.

4
New cards

Powerful

Control creation, property access, printing, cloning and more.

5
New cards

__construct

runs when an object is created; use it to set initial state.

6
New cards

__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.

7
New cards

Constructor promotion (PHP 8)

declares and assigns properties in one step inside the constructor.

8
New cards

__get

called when reading an inaccessible (private/undefined) property.

9
New cards

__set

called when writing an inaccessible (private/undefined) property; great for validation and controlled access.

10
New cards

__toString

defines how an object behaves as a string (echo, concatenation); must return a string.

11
New cards

__invoke

lets you call an object like a function; suits callbacks like array_map().

12
New cards

__call

handles calls to undefined or inaccessible instance methods.

13
New cards

__callStatic

does the same as __call but for static methods; useful for dynamic APIs and proxies.

14
New cards

__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).

15
New cards

__isset

runs when _____ or empty() is used on an inaccessible property; without it, _____ on hidden properties returns false.

16
New cards

__unset

runs when ____ is used on an inaccessible property.

17
New cards

Exception

an unexpected event that interrupts normal program flow (e.g. file not found, division by zero, database error).

18
New cards

try

the block where you place code that might cause an error.

19
New cards

catch

handles the error if one happens.

20
New cards

finally

runs always, whether or not an error happened.

21
New cards

DivisionByZeroError

thrown when dividing a number by zero.

22
New cards

TypeError

thrown when passing the wrong data type to a function.

23
New cards

ValueError

thrown when a value has a valid type but an invalid value.

24
New cards

ParseError

thrown for syntax errors in code.

25
New cards

Throwable

the parent of both Exception and Error; catching it catches both.

26
New cards

Use specific exception classes

create different custom exceptions for different errors.

27
New cards

Catch only what you can handle

avoid catching exceptions you can’t properly handle.

28
New cards

Use finally for cleanup

always use finally to close files, DB connections, or show a message.

29
New cards

Avoid silent fails

never catch errors without logging or notifying.

30
New cards

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.

31
New cards

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.

32
New cards

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.

33
New cards

Avoids conflicts (namespace benefit)

prevents name collisions between classes/functions.

34
New cards

Organized structure (namespace benefit)

groups related code logically.

35
New cards

Readable code (namespace benefit)

makes a class’s purpose and location obvious.

36
New cards

Autoloading support (namespace benefit)

works seamlessly with Composer and PSR-4.

37
New cards

Team-friendly (namespace benefit)

reduces merge and naming issues in teams.

38
New cards

Reusable modules (namespace benefit)

lets you reuse components across projects.

39
New cards

Safe package use (namespace benefit)

allows multiple third-party libraries to be used safely together.

40
New cards

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.

41
New cards

spl_autoload_register()

lets you register a custom function that tells PHP how to find and load class files whenever a class is used.

42
New cards

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.

43
New cards

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.

44
New cards

composer.json

the file where you declare the root namespace prefix (e.g. “App\”) and the folder it maps to (e.g. “src/App/”).