0

I noticed following notation in PHP. What does it mean?

$locales = GeneralUtility::makeInstance(Locales::class);

i have problems understanding parameter of makeInstance. Locales seems to be another static class. What means notation of

Locales::class

explicitly? What kind of parameter will it expect?

tklustig
  • 483
  • 6
  • 24
  • Well, would be crazy, calling variable or method as 'class', wouldn't it? Thought, 'class' is reserved as keyword for classes?! – tklustig Sep 15 '18 at 07:53
  • 1
    Possible duplicate of [Reference — What does this symbol mean in PHP?](https://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Nigel Ren Sep 15 '18 at 07:53
  • @MadhurBhaiya you're wrong. It's a reference to the full namespaced class name. – Evert Sep 15 '18 at 11:31

1 Answers1

0

According to the documentation, http://php.net/manual/en/migration55.new-features.php#migration55.new-features.class-name, you can use it to get the fully qualified name of a class.

In your case, let's assume the Locale class is defined like this:

file1.php

<?php
    namespace App\Core;

    class Locale {}

    echo Locale::class;     // output - App\Core\Locale

In a separate file, you could require/autoload the file,

file2.php

<?php

    require_once #pathToFile1/file1.php;

    use App\Core\Locale;

    echo Locale::class;        // output - App\Core\Locale

We can also see from this that we no longer have to save our class name in a variable as we can always call Locale::class and get going.

To address one of the comments on the question, yes, class is a keyword and using it for function name usually result in a parse error.. That changed in PHP 7 though...thus you can define a function and name it class, function etc. It's best not to do this though unless you think it's absolutely necessary.

Chukwuemeka Inya
  • 2,575
  • 1
  • 17
  • 23