0
0
PHPprogramming~10 mins

Importing with use keyword in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Importing with use keyword
Start PHP file
Declare namespace or global
Use 'use' keyword to import class/namespace
Create object or call method using imported name
Run code with imported class
End
The 'use' keyword imports classes or namespaces so you can use shorter names in your code.
Execution Sample
PHP
<?php
use My\Tools\Calculator;
$calc = new Calculator();
echo $calc->add(2, 3);
?>
This code imports Calculator class and uses it to add two numbers.
Execution Table
StepActionCode LineEffectOutput
1Start PHP file<?phpPHP starts parsing
2Import Calculator classuse My\Tools\Calculator;Calculator class is imported with alias 'Calculator'
3Create Calculator object$calc = new Calculator();Object $calc created from My\Tools\Calculator
4Call add methodecho $calc->add(2, 3);Method add(2,3) called, returns 55
5End script?>Script ends
💡 Script ends after outputting the sum 5
Variable Tracker
VariableStartAfter Step 3After Step 4Final
$calcundefinedCalculator object instanceCalculator object instanceCalculator object instance
Key Moments - 2 Insights
Why do we use the 'use' keyword instead of writing full class names every time?
The 'use' keyword lets us write shorter names for classes, making code easier to read and write, as shown in step 2 and 3 of the execution_table.
Does 'use' actually load the class code?
'use' only imports the class name for easier reference; the class code loads when you create an object or call a method, like in step 3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output at step 4?
A2
B3
C5
DError
💡 Hint
Check the Output column at step 4 in the execution_table.
At which step is the Calculator object created?
AStep 3
BStep 2
CStep 4
DStep 5
💡 Hint
Look at the Action column for object creation in the execution_table.
If we remove the 'use' line, what will happen when creating the Calculator object?
AIt will work the same
BPHP will throw an error about class not found
CThe object will be created from global namespace
DThe code will output 0
💡 Hint
Without 'use', PHP cannot find the class name as shown in step 2 explanation.
Concept Snapshot
PHP 'use' keyword imports classes or namespaces.
It allows shorter names instead of full paths.
Use it at the top of your PHP file.
Imported names can be used to create objects or call methods.
It improves code readability and organization.
Full Transcript
This visual execution shows how PHP imports a class using the 'use' keyword. First, the PHP file starts parsing. Then, the 'use' statement imports the Calculator class from the My\Tools namespace, allowing us to use the short name 'Calculator'. Next, we create an object of Calculator. Then, we call the add method with 2 and 3, which returns 5. Finally, the script ends after outputting 5. The variable $calc holds the Calculator object after creation. The 'use' keyword does not load the class code itself but makes the class name available for use. Without 'use', PHP would not find the class and throw an error.