Class abstraction??? maybe

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jayaremc
Forum Newbie
Posts: 1
Joined: Wed Sep 30, 2009 7:00 pm

Class abstraction??? maybe

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Class abstraction??? maybe

Post 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?
Post Reply