📖 Scenario: Imagine you are writing a PHP script that uses a function named strlen to find the length of a string. However, you also have a custom function named strlen inside a namespace. You want to use the original PHP strlen function from the global namespace.
🎯 Goal: You will create a namespaced function with the same name as a global PHP function, then learn how to call the global function explicitly using the global namespace access.
📋 What You'll Learn
Create a namespace called MyApp
Inside MyApp, create a function called strlen that returns a fixed number
Create a variable $text with the value 'Hello'
Call the global strlen function on $text using global namespace access
Print the result of the global strlen function call
💡 Why This Matters
🌍 Real World
Namespaces help organize code and avoid name conflicts in large PHP projects.
💼 Career
Understanding namespaces and global namespace access is important for working with modern PHP frameworks and libraries.
Progress0 / 4 steps
1
Create a namespace and a custom strlen function
Create a namespace called MyApp. Inside it, define a function called strlen that takes one parameter $str and returns the number 42.
PHP
Hint
Use the namespace keyword to create a namespace. Define the function inside curly braces.
2
Create a variable with a string value
Outside the MyApp namespace, create a variable called $text and set it to the string 'Hello'.
PHP
Hint
Just create a variable $text and assign the string 'Hello' to it.
3
Call the global strlen function using global namespace access
Create a variable called $length and set it to the result of calling the global strlen function on $text using the global namespace access operator \.
PHP
Hint
Use \strlen($text) to call the global PHP function strlen explicitly.
4
Print the length from the global strlen function
Print the value of the variable $length using echo.
PHP
Hint
Use echo $length; to display the length.
Practice
(1/5)
1. What does the leading backslash \ do when used before a function name inside a PHP namespace?
easy
A. It creates a new function inside the current namespace.
B. It comments out the function call.
C. It calls a function from the current namespace only.
D. It accesses the function from the global namespace.
Solution
Step 1: Understand namespaces in PHP
Namespaces group code to avoid name conflicts but can hide global functions.
Step 2: Role of leading backslash
The leading backslash tells PHP to look for the function in the global namespace, ignoring the current namespace.
Final Answer:
It accesses the function from the global namespace. -> Option D
Quick Check:
Leading backslash = global access [OK]
Hint: Leading \ means use global function, not namespaced one [OK]
Common Mistakes:
Thinking \ creates a new function
Assuming \ calls current namespace function
Confusing \ with escape character
2. Which of the following is the correct syntax to call the global strlen function inside a namespace?
easy
A. \strlen('test');
B. namespace\strlen('test');
C. strlen('test');
D. global strlen('test');
Solution
Step 1: Identify global function call syntax
To call a global function inside a namespace, prefix it with a backslash \.
Step 2: Check each option
\strlen('test'); uses \strlen('test'); which correctly calls the global strlen function.
Final Answer:
\strlen('test'); -> Option A
Quick Check:
Backslash before function = global call [OK]
Hint: Use \ before function name to call global version [OK]