This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 9 years ago.
Preparing for the ZEND-Exam, I found a question where a class redefined the strlen-function:
namespace MyFramework\MyString
{
function strlen ($str)
{
return \strlen($str) * 2; // return double string-length
}
}
I never came across that "\function"-thing before and could not get an explanation from my trainer - can somebody pls. help shed some light...?
It calls a function from the global namespace.
You need it only if there's a function of the same name in the current namespace.
Introduced in PHP 5.3, \ is a namespace separator.
In your example, the outer strlen is MyFramework\MyString\strlen, the inner strlen is the global one.
Without any namespace definition, all class and function definitions are placed into the global space - as it was in PHP before namespaces were supported. Prefixing a name with \ will specify that the name is required from the global space even in the context of the namespace.
Reference: http://www.php.net/manual/en/language.namespaces.global.php
Related
This question already has answers here:
PHP | What are three dots before a function arguments?
(1 answer)
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 3 years ago.
What is the real purpose of function with ellipsis in its parameter?
I have this function:
class Dog{
public function type(...$numbers){
var_dump($numbers);
}
}
and this function
class Dog{
public function type($numbers){
var_dump($numbers);
}
}
Whether I put ellipsis or not, if I call the type function putting multiple parameters in it, its type will always be an array.
So my question is, why exactly should I put the ellipsis inside the parameter of a function?
It's just syntactic sugar, called variable-length argument lists. It lets you pass the function multiple arguments that it will turn into an array automatically. In that example, it would let you call type(1, 2, 3) and $numbers would be an array of those three numbers.
This question already has answers here:
php - Why can't you define a constant named EMPTY
(4 answers)
Closed 4 years ago.
Could you please tell me why this code throws a parse error if the name of the constant is EMPTY, but if I change it to EMPTY2 or SUBSTR it does work.
define('EMPTY', '');
if (empty(EMPTY)) {
echo 'hello world';
}
Because, as stated in this Quora answer, PHP function names are case-insensitive, so EMPTY collides with the built-in function empty().
PHP manual mentions this in a small note right after Example #3 in the subsection about User-defined functions:
Note: Function names are case-insensitive, though it is usually good form to call functions as they appear in their declaration.
This question already has answers here:
What does ClassName::class mean in PHP? [duplicate]
(3 answers)
Closed 7 years ago.
I noticed in Laravel this syntax:
Illuminate\Foundation\Providers\ArtisanServiceProvider::class
What does the ::class operator do?
from PHP doc
"Since PHP 5.5, the class keyword is also used for class name resolution. You can get a string containing the fully qualified name of the ClassName class by using ClassName::class. This is particularly useful with namespaced classes."
<?php
namespace NS {
class ClassName {
}
echo ClassName::class;
}
?>
http://php.net/manual/en/language.oop5.basic.php
From the docs:
Since PHP 5.5, the class keyword is also used for class name resolution. You can get a string containing the fully qualified name of the ClassName class by using ClassName::class. This is particularly useful with namespaced classes.
This question already has answers here:
What does it mean to start a PHP function with an ampersand?
(3 answers)
Closed 7 years ago.
I'm using a CMS package written in PHP. In one of it's core files I saw following line that is for defining a function in a class body.
public static function &getLib($sClass, $aParams = array()) {
// Code
}
I didn't understand why the function name 'getLib' has been prepended with the ampersand(&) sign? I've never seen such thing before.
Can someone please explain me in detail why such thing has been done and what's the benefit it has over simply using the function name?
It means the function should return a reference to a variable rather than just the value itself.
This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 8 years ago.
Can you please explain to me the differences between two functions:
function &a(){
return something;
}
and
function b(){
return something;
}
Thanks!
The first returns a reference to something, the second a copy of something.
In first case, when the caller modify the returned value, something will be modified as a global variable do.
In the second case, modifying a copy as no effect to the source.
An ampersand before a function name means the function will return a reference to a variable instead of the value.
According to this LINK
Returning by reference is useful when you want to use a function to find to which
variable a reference should be bound. Do not use return-by-reference to increase
performance. The engine will automatically optimize this on its own. Only return
references when you have a valid technical reason to do so.