Viewed   90 times

I'm trying to access a class constant in one of my classes:

const MY_CONST = "value";

If I have a variable which holds the name of this constant like this:

$myVar = "MY_CONST";

Can I access the value of MY_CONST somehow?


does not work obviously because it is for static properties. Variable variables does not work either.



There are two ways to do this: using the constant function or using reflection.

Constant Function

The constant function works with constants declared through define as well as class constants:

class A
    const MY_CONST = 'myval';

    static function test()
        $c = 'MY_CONST';
        return constant('self::'. $c);

echo A::test(); // output: myval

Reflection Class

A second, more laborious way, would be through reflection:

$ref = new ReflectionClass('A');
$constName = 'MY_CONST';
echo $ref->getConstant($constName); // output: myval
Wednesday, October 26, 2022

You probably don't really want to be doing this, as it's going to be a nightmare to debug, but it seems to be possible. The key is the part where you assign by reference in the constructor.

$GLOBALS = array(
    'MyNumber' => 1

class Foo {
    protected $glob;

    public function __construct() {
        global $GLOBALS;
        $this->glob =& $GLOBALS;

    public function getGlob() {
        return $this->glob['MyNumber'];

$f = new Foo;

echo $f->getGlob() . "n";
$GLOBALS['MyNumber'] = 2;
echo $f->getGlob() . "n";

The output will be


which indicates that it's being assigned by reference, not value.

As I said, it will be a nightmare to debug, so you really shouldn't do this. Have a read through the wikipedia article on encapsulation; basically, your object should ideally manage its own data and the methods in which that data is modified; even public properties are generally, IMHO, a bad idea.

Friday, August 19, 2022

You have to use $this->interval to access the member variable interval in PHP. See PHP: The Basics

class DateFilter extends Filter
    private $interval;    // this is correct.

    public function DateFilter($daysOld)
        $this->interval = new DateInterval('P'.$daysOld.'D');   // fix this

    function test()
        echo $this->interval->format("%d days old </br>");     // and fix this
Thursday, December 15, 2022
$variable = $classname.'::'.$constant;


See the docs:

Wednesday, December 21, 2022

Here is an example to create a custom element

  export default class ListBox extends HTMLElement {
  // observe attributes of custom element
  static get observedAttributes() {
    return ["disp", "text"];

  get myAction() {
    return this.hasAttribute("open");

  set myAction(val) {
    if (val) {
      this.setAttribute("open", "");
    } else {

  constructor() {
    this.initialized = false;
    this.div = document.createElement("div");
    // get and assigns value from props "disp" = this.getAttribute("disp");
    // get and assigns value from props "text"
    this.div.innerHTML = this.getAttribute("text");
  connectedCallback() {
    // didMount
    // your methode here
    this.initialized = true;
    console.log("custom element added to page");

  disconnectedCallback() {
    // unmount
    console.log("custom element remove from page");

  attributeChangedCallback(name, oldValue, newValue) {
    // observed props
    console.log("props", arguments);
    // get and assigns value from props "text"
    if (name === "text" && oldValue !== newValue) {
      this.div.innerHTML = newValue;

in your html in the script tag which calls your index.js add type="module"

<!DOCTYPE html>

    <div id="app">
      <list-box text="Some text here" disp="block"></list-box>
    <script src="src/index.js" type="module"></script>

In your index.js import your component and do Something

import ListBox from "./component/ListBox";

customElements.define("list-box", ListBox);

// change props "text value"
  .setAttribute("text", `Change the value of props "text"`);
Wednesday, November 16, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :