Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference static::class vs get_called_class() and __CLASS__ vs get_class() vs self::class

Tags:

oop

php

class

I've seen several threads where people ask how to get the name of a class or an object in PHP. However, I can't see anywhere the difference between the various possibilities explained. I hope somebody here can help me.

So in order to get the class name of the called class, I know two possibilities:

  1. get_called_class()

  2. static::class

(get_class($this) for non-static classes)

And for getting the class name of the class in which you put the code, I know these three possibilities:

  1. get_class()

  2. __CLASS__

  3. self::class

Are there any differences which I may to overlook right now? What are potential adventages and disadvantages of one way over the other?

like image 992
Alexander Jank Avatar asked Dec 13 '17 17:12

Alexander Jank


People also ask

What does :: class do in PHP?

::class ¶ The class keyword is also used for class name resolution. To obtain the fully qualified name of a class ClassName use ClassName::class . This is particularly useful with namespaced classes.

How do I find the instance of an object in PHP?

PHP | get_class() Function Return Value: This function returns the class name of which object is an instance. It returns FALSE if the object is not an object. If the object is omitted when inside the class then the class name is returned. $obj = new GFG();


2 Answers

Differences between

get_class()

returns the name of the class of an object

It returns a classname including its qualified namespace for the current class (without parameter) or for any specified object instance, when you pass the object instance pointer as the first and only parameter.

__CLASS__

A magic constant that returns the qualified namespace and the current class name. Here you can not test for class names of other objects. As per PHP 5.4 it works in traits. That is, when the trait is used in a class it will return the namespace and name of that class.

::class

Only available since PHP 5.5. It uses class name and namespace resolution to obtain the info, as a result it does not need the class to be instantiated beforehand. Also 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.

Tests

<?php
namespace nTest;
trait tTest {
  function __toString() {return get_class();}
  function className() {return __CLASS__;}  // per PHP 5.4
  function traitName() {return __TRAIT__;}
}
class cTest {
  use tTest;
  function usedTraitName() {return __TRAIT__;}
}
class cClassWithoutObject {}
$oTest = new cTest;

header('Content-type: text/plain');
print                                  // Output:
    $oTest . PHP_EOL                   // 'nTest::cTest'
  . get_class($oTest) . PHP_EOL        // 'nTest::cTest'
  . $oTest->className() . PHP_EOL      // 'nTest::cTest'
  . $oTest->traitName() . PHP_EOL      // 'nTest::tTest' (trait!)
  . $oTest->usedTraitName() . PHP_EOL  // '' (no trait!)
  . cTest::class . PHP_EOL             // 'nTest::cTest'
  . cClassWithoutObject::class;        // 'nTest::cTestNotInstantiated'
like image 158
Code4R7 Avatar answered Oct 11 '22 19:10

Code4R7


One difference between __CLASS__ and get_class($this) that's important if you're using inheritance is that __CLASS__ will give you the name of the class containing that code (which could be a parent class), whereas get_class($this) will give you the name of the concrete class of $this at runtime.

Example:

class Animal {

    function printCompileTimeType() {

        echo __CLASS__;
    }

    function printOverridableCompileTimeType() {

        echo __CLASS__;
    }

    function printType() {

        echo get_class($this);
    }
}

class Dog extends Animal {

    function printOverridableCompileTimeType() {

        echo __CLASS__;
    }
}

$animal = new Animal();
$dog = new Dog();

$animal->printCompileTimeType(); // Prints 'Animal'.
$animal->printOverridableCompileTimeType(); // Prints 'Animal'.
$animal->printType(); // Prints 'Animal'.

$dog->printCompileTimeType(); // Prints 'Animal'.
$dog->printOverridableCompileTimeType(); // Prints 'Dog'.
$dog->printType(); // Prints 'Dog'.

I'd say get_class($this) is generally more useful because it will work predictably no matter where you use it from, without needing to write repetitive override code like printOverridableCompileTimeType.

like image 27
Andrew Koster Avatar answered Oct 11 '22 17:10

Andrew Koster