A
A
Agelios2018-11-07 16:14:25
PHP
Agelios, 2018-11-07 16:14:25

How to get private const conveniently?

There is a class in which a number of private constants are described.

class Test {
    private const ONE = 1;
    private const TWO = 2;
...
}

and accordingly there are functions to work
class Test
{
....
    public function all()
    {
        return [
            self::ONE => 'один',
            self::TWO => 'два'
        ]
    }

    public function getOne()
    {
        return self::ONE;
    }
...
}

but soon this class may be flooded with many such constants and it is not necessary to write such functions every time. What can you think of?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
@
@Construct, 2018-11-07
@Agelios

Looks like an enum type.
This library does some of the work for you and adds type checking: https://github.com/myclabs/php-enum

A
Alexander Pushkarev, 2018-11-07
@AXP-dev

1) Make the constants public, they still cannot be changed. Encapsulation will not be broken.
2) Write wrappers through reflection

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question