what is ::class php in laravel [duplicate] - php

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

Related

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: What does ::class do? [duplicate]

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.

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

What does :: mean in php? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
what is the “::” notation in php used for?
I noticed this code while modifying a friends code and noticed this piece of code: TestPages::LoadMenu();
what does :: mean in php?
A great answer would mean a lot.
Thanks!
It's the 'Scope Resolution Operator'.
The Scope Resolution Operator (also called Paamayim Nekudotayim) or in
simpler terms, the double colon, is a token that allows access to
static, constant, and overridden properties or methods of a class.
http://php.net/manual/en/language.oop5.paamayim-nekudotayim.php
In layman terms it is used to call static Methods of a Class.
In your example, LoadMenu() is a static function of the TestPages class.
This means that you do not have to create an instance of a TestPages to call LoadMenu()
It is used to access static methods of class, static variables and constants
Read more
It means static class member access, in this case static method invocation.
It's used to access class methods / properties:
http://www.php.net/manual/en/language.oop5.paamayim-nekudotayim.php

create class directly after a property field? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
declare property as object?
in java you can create an object directly after the property field like this:
but it seems not working for php:
class Test {
public $object = new Object();
}
you have to create it in the __construct() and assign it to the property?
thanks
From php.net
This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.
So no, you cant initialize it to an object. You'll have to do it in the constructor like you said

Categories