PHP: What does ::class do? [duplicate] - php

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.

Related

Get a list of constants outside of PHP class [duplicate]

This question already has answers here:
Find or list all constants used in a PHP file
(2 answers)
Closed 3 years ago.
I'm trying to get a list of all the constants that are defined outside of a PHP class but only if they start with a specific prefix (see this tutorial) and saw the ReflectionClass::getConstants functionality but this requires a class.
How do I achieve the same functionality if the constants are defined outside of a class?
You can use the built-in function get_defined_constants. It will list all the constants even the ones coming from Core PHP and loaded extensions. You can pass true as the parameter to categorize them. To get just userland constants (both from const and from define()) use get_defined_constants(true)['user']
<?php
define('DEFINED_CONST', 'foo');
const myConst = 1;
print_r(get_defined_constants(true)['user']);
prints:
Array
(
[DEFINED_CONST] => foo
[myConst] => 1
)

what is ::class php in laravel [duplicate]

This question already has answers here:
What is ::class in PHP?
(4 answers)
Closed 4 years ago.
Sorry for weak English!
I know ClassName::Class will return class name with namespace.
but why in laravel used this when class name is written with namespace?
like:
\App\Http\Middleware\TrimStrings::class
I'm confused about it!
Can someone tell me what is ::Class exactly?
Answer:
Using that ::class syntax allows you to declare a class without
passing it as a string. When they update ide's it will help to auto
fill namespaces which can't be done with a string.
Source Information:
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.
Example #10 Class name resolution
<?php
namespace NS {
class ClassName {
}
echo ClassName::class;
}
?>
The above example will output:
NS\ClassName
Note:
The class name resolution using ::class is a compile time
transformation. That means at the time the class name string is
created no autoloading has happened yet. As a consequence, class names
are expanded even if the class does not exist. No error is issued in
that case.
Source Link :: Php Manual

What is the meaning of "$cm->course" in moodle? [duplicate]

This question already has answers here:
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 6 years ago.
I am new at PHP and currently now i am working on moodle LMS tool.
I am referring view.php of scorm package and dont know why $cm->course this is used in php.
Please help.
Thanks in Advance
Have a look here
"->" means accessing the $cm object property course
$cm is an object, and course is a variable. Here you can see brief of PHP syntax with objects:
http://php.net/manual/en/language.types.object.php
PHP has two Object Operator namely -> and ::
-> is used when you are trying to call a method on an Instance and / or access an Instance property.
:: is used when you want to call a static method or call a parent
class's version of a method within a child class.
Reference: https://www.codeproject.com/questions/196270/what-is-meant-by-symbol-in-php
Note: I googled your exact question and got this result!
It is "object operator" - T_OBJECT_OPERATOR. It is used to access properties and methods of class on an object.

PHP __CLASS__ like magic constant respect inheritance? [duplicate]

This question already has answers here:
How to get the name of child class from base class when an object of child class is created
(4 answers)
Closed 7 years ago.
Is there a magic constant which, when used in a parent class, will return its child's class name, and not the parent?
What you need is a late static binding.
Not sure if you need a constant only. But using get_class($this); in parent you will get the class of the caller(parent or child)

What does backslash do as prefix to a function name? [duplicate]

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

Categories