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.
$a = [1, 2, 3, 4];
or$a = ['one' => 1, 'two' => 2, 'three' => 3, 'four' => 4];
– Linblow Mar 12 at 15:03