Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6947705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:43:38+00:00 2026-05-27T13:43:38+00:00

I was trying to find a method to handle enumeration in PHP. None of

  • 0

I was trying to find a method to handle enumeration in PHP.
None of the approaches I found can actually use enumeration to limit the arguments to functions/methods, which is the main reason I want enums.

So I started trying to create my own, but have now run into a problem using reflection:

<?php

class Enum {
    private $_value;
    protected function __construct($value) {
        $this->_value = $value;
    }

    public function getValue() {
        return $this->_value;
    }

    protected static function enumerate($class) {
        $ref = new ReflectionClass($class);
        $instances = array();
        foreach ($ref->getStaticProperties() as $name => $value) {
            $ref->setStaticPropertyValue($name, new $class($value));
        }
    }
}

class Book extends Enum {
    public static $COMIC = 1;
    public static $NOVEL = 2;
    public static $EDUCATIONAL = 3;

    public static function enumerate() {
        parent::enumerate(__CLASS__);
    }
}
Book::enumerate();

function read(Book $book) {
    echo '<hr/>';
    var_dump($book);
}

read(Book::$COMIC);
read(Book::$EDUCATIONAL);

?>

I expected output to show two Book objects with their respective values, instead I got this:

object(Book)#2 (1) { ["_value:private"]=> object(Book)#2 (1) { ["_value:private"]=> *RECURSION* } }
object(Book)#4 (1) { ["_value:private"]=> object(Book)#4 (1) { ["_value:private"]=> *RECURSION* } }

The value (new $class) going into setStaticPropertyValue is correct and there are created only three of these.

getStaticProperties only picks up the static properties (as it should) and the only place the private $_value is assigned is in the constructor.
For completeness sake, the constructor is called only three times and inside the constructor, the value is, as expected, only ever 1, 2 or 3. In short, everything seems to be perfectly fine until the calls to setStaticPropertyValue.

I can’t figure out what is going on here. Where and why is the private $_value assigned this wrong value? Am I using reflection the wrong way? What is creating these infinite recursive objects?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T13:43:38+00:00Added an answer on May 27, 2026 at 1:43 pm

    This is fascinating. I love it when I run across things that push the boundaries of PHP.

    I have to say, though, that this seems a bit extreme, and depending on how fast reflection is, it might be fairly slow.

    For a framework I wrote, I created a simple enum function. All it’s doing is automating defines. I like your approach better because you can type-restrict.

    Your code works for me. I’m posting my edits below – they are mainly formatting changes so I could read it more easily.

    Check out what I’ve done to it. I tested this with 5.3.5 and 5.2.17 – works fine on either one.

    <?php
    header( 'content-type: text/plain' );
    
    Book::enumerate();
    Book::$COMIC->read();
    read(Book::$EDUCATIONAL);
    read(Book::$NOVEL );
    
    class Enum
    {
        private $_value;
        protected function __construct($value)
        {
            $this->_value = $value;
        }
    
        public function getValue()
        {
            return $this->_value;
        }
    
        protected static function enumerate($class)
        {
            $ref = new ReflectionClass($class);
            $instances = array();
    
            foreach ( $ref->getStaticProperties() as $name => $value )
            {
                $ref->setStaticPropertyValue( $name, new $class( $value ) );
            }
        }
    }
    
    class Book extends Enum
    {
        public static $COMIC = 'comic';
        public static $NOVEL = 'novel';
        public static $EDUCATIONAL = 'edu';
    
        public static function enumerate()
        {
            parent::enumerate(__CLASS__);
        }
        public function read()
        {
            echo "\nReading a {$this->getValue()} book\n";
        }
    }
    
    function read( Book $book )
    {
        echo "\nReading a {$book->getValue()} book\n";
    }
    

    Output

    Reading a comic book
    
    Reading a edu book
    
    Reading a novel book
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write (or just find an existing) PHP method that can take
Trying to find an XML file I can use in lieu of a look-up
i've been trying to find a method in C# to measure the size of
Trying to find a good name for a method swapping each coordinate X and
I'm trying to implement method Find that searches the database. I forgot to mention
I'm trying to find a way to fake the result of a method called
I'm trying to find the jQuery equivalent of this JavaScript method call: document.addEventListener('click', select_element,
I'm trying to find proper way to handle stale data on NFS client. Consider
I'm trying to handle errors, and I find it very hard to know what
I am trying to find the perfect way to handle this exception and force

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.