1

I have been trying to write a class to make inserting items into a database a little easier for my classmates. To do so I want to make a function in that class that will take all columns and values as an array of keys and values. Obviously I could do the following:

class aClass {
    public function aFunction( $args ) {
    }
}

(new aClass)->aFunction( Array( 'column1' => 'value1', 'column2' => 'value2' ) );

That would create an array $args with $args[column] = value. And I know I am not the first person to ask this but I want it fancier damnit. I am looking for an alternative that will at least be fancier than the above example. It didn't take long for me to realise that '=>' is a straight up syntax error for every class besides Array. So then, I have tried two more things, one of which was worse and one of which didn't work because my reasoning failed.

class aClass {
    public function aFunction( ) {
        $args = func_get_args();
    }
}

(new aClass)->aFunction( 'value1', 'value2' );

The above is pretty neat, however I know of no way to pass the column names without making it longer and stupider than the first example.

class aClass {
    public function aFunction( ) {
        $args = get_defined_vars();
    }
}

(new aClass)->aFunction( $column1 = 'value1', $column2 = 'value2' );

The above would be alright, besides the fact that it doesn't work. I assume it doesn't work because "$column1 = 'value1'" would just return "'value'" on succes, which makes it a stupidly long and resource inefficient way of just writing "'value'".

Any sugestions? I know it's kinda stupid and the question has probably been asked before but it just seems unfair that only the Array class gets to have that syntaxis. I have actually been googling for hours right now, dear god please help my ocd.

edit: Even if it is just something to make the php parser interpret => as a comma so I can just use the modulus operator, anything is better.

2
  • 1
    You could use the new short array notation (PHP >=5.4). Example: $a = [1, 2, 3, 4]; or $a = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4]; Commented Mar 12, 2014 at 15:03
  • Oh snap that's actually not too bad :I Commented Mar 12, 2014 at 15:16

1 Answer 1

1

You could use something like this as a template, it still needs some work.

<?php

class DB{
        private $cols;
        private $values;


        public function __call($method,$arguments){
                $this->cols[] = "`".$method."`=?";
                $this->values[] = $arguments[0];
                return $this;
        }

        public function exec(){
                $sql = "INSERT INTO `test` SET ";
                $sql .= implode(",",$this->cols);
                echo "SQL for prepared statement: ".$sql."\n";
                echo "Values:\n";
                print_r($this->values);
        }


        public static function insert(){
                return new self();
        }

}

//Usage    
DB::insert()->column1('value1')->column2('value2')->exec();

Output:

SQL for prepared statement: INSERT INTO `test` SET `column1`=?,`column2`=?
Values:
Array
(
    [0] => value1
    [1] => value2
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.