PHP 8.5.0 RC 2 available for testing

Voting

: zero minus zero?
(Example: nine)

The Note You're Voting On

Sandor Toth
8 years ago
You can pass $this as class for the ReflectionClass. __CLASS__ won't help if you extend the original class, because it is a magic constant based on the file itself.

<?php

class Example {
const
TYPE_A = 1;
const
TYPE_B = 'hello';

public function
getConstants()
{
$reflectionClass = new ReflectionClass($this);
return
$reflectionClass->getConstants();
}
}

$example = new Example();
var_dump($example->getConstants());

// Result:
array ( size = 2)
'TYPE_A' => int 1
'TYPE_B' => (string) 'hello'

<< Back to user notes page

AltStyle によって変換されたページ (->オリジナル) /