Page 1 of 1

Class abstraction??? maybe

Posted: Wed Sep 30, 2009 7:15 pm
by jayaremc
Not sure how to ask this question but here goes.

A class implementation of ENUM within PHP the class can be declared and used as such:

enum('Gender', array('Male', 'Female'));
$g = Gender::Male();

To me this appears that Male is now a function of a class called gender that was created/named based on a passed in string.

and simpleXML in PHP can take a GPX file and all of sudden have, what appears to me to be, functions that use the names of nodes in the file. Once again it appears to me that PHP was able to take a string and create a class with that same string value.

The enum code which I can somewhat read has the following

Code: Select all

 
foreach ($args as $k => $enum) {
        $static_method = 'public static function ' . $enum .
            '() { return ' . $enum . '::instance(); }';
        $enums[$static_method] = '
            class ' . $enum . ' extends ' . $base_class_name . '{
                private static $instance = null;
                protected $value = "' . addcslashes($k, '\\') . '";
                private function __construct() {}
                private function __clone() {}
                public static function instance() {
                    if (self::$instance === null) { self::$instance = new self(); }
                    return self::$instance;
                }
            }';
 
where $enum is the array elements and $base_class_name is the ENUM name.

What is happening here and as far as a google search to learn more of what is happening here what are the correct/effective keywords to use to get useful results.

Re: Class abstraction??? maybe

Posted: Wed Sep 30, 2009 8:19 pm
by requinix
The method you're using - constructing the code for a PHP class - is not the way to do this. Not even close.

I can't see any good reason why you would want to do an enum using a class. What's the purpose?