Jump to content

Variable Pointing To a Class Address?


MrFish

Recommended Posts

I'm trying to write an sql parsing tool for specific purposes. I'm going to run all the sql statements through mysql but any create statement needs to update the table if there are new fields. So it doubles as an alter statement. The problem I'm having is a simple one but I'm wonder how this could be possible in PHP. Each statement I parse out will be called a Command and extend the SQLCommand super class. I'm going to determine what command it's using by looping through each word of a command, adding it to a string where I try to match it to an associative array.

        class SQLCommand        {                private $sql;                private $executables;                 //String                private $command;                private $table;                 //String[]                private $flags;                private $parameterSets;                  function SQLCommand($str)                {                        $this->sql = str;                }                 function getExecutables()                {                        return $this->executables;                }        }         class SQLCreate extends SQLCommand        {         }

CREATE TABLE IF NOT EXISTS `builders` (  `bld_id` int(11) NOT NULL AUTO_INCREMENT,  `bld_name` varchar(30) NOT NULL,  `bld_brandLogo_med` mediumint(9) NOT NULL,  `bld_brandLogo_sm` mediumint(9) NOT NULL,  `bld_reportingName` varchar(75) NOT NULL,  `bld_defaultLeadsEmail` varchar(100) NOT NULL,  `bld_copyLeadsEmail` varchar(100) NOT NULL,  `bld_builderWebsite` varchar(255) NOT NULL,  `bld_LeadsPerMessage` enum('All','1') NOT NULL,  PRIMARY KEY (`bld_id`)) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=0 ;

This would look for "CREATE" then "CREATE TABLE" (where it will make a match). I could create a long if-else or switch to choose what Command to pass back but here is what I'd like to do-

                function parseCommands()                {                        $commandRegex = "|([^;]+)|";                        preg_match_all($commandRegex, $this->sql, $matches);                        foreach($matches[1] as $match)                        {                                $this->commands[] = self::SQLCommandFactory($match);                        }                }                 ....                 public static function SQLCommandFactory($commandSQL)                {                        $knownCommands = array(                                                "CREATE TABLE" => &SQLCreate;                                              );                         print_r($knownCommands);                         return NULL;                }

Is this possible? So I can eventually do $CommandCls &= $knownCommands[$commandStr];$Command = new *$CommandCls($commandStr); My pointers might be off. Haven't had much practice with them.

Link to comment
Share on other sites

I'm doing some tests now. How would I do this if possible? Trying to avoid eval.

 <?php        class A        {                function __toString(){return "A";}        }         $Class = A;        $Instance = new $Class($parameters = NULL);         echo "No Error";?>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...