re: What are your favorite programming language syntax features? VIEW POST


I'm not sure if I like it really, and I think its an anti-pattern in almost all cases, and some of it can be done via reflection in most languages, and I don't know the name for it...

This BS in PHP:

class Foo {
    public function bar() : int {
        return 42;

$functionName = 'bar';
$foo = new Foo();
echo $foo->{$functionName}(); //42
$instanceName = 'foo';
echo $$instanceName->bar(); //42
echo $$instanceName->{$functionName}(); //42

$variableName = 'blackMagic';
$$functionName = 'whut?';
echo $blackMagic; //'whut?'

This comes from function pointer in C. You can create a function pointer, like this:

void (* init_sgbd_ptr)(void);
void (* connect_sgbd_ptr)(const char* const);

Let's say that we have an application which could connect to either MySQL or PostgreSQL. In a config file, the software could detect which DBMS is used, and then link all the DBMS interface correctly. Like this

// PostgreSQL interface
void postgres_init(void) { ... }
void postgres_connect(const char* const dsn) { ... }

// MySQL interface
void mysql_init(void) { ... }
void mysql_connect(const char* const dsn) { ... }

So, in the DBMS manager module:

void init_dbms(dbms_type_t dbms_type)
    switch (type) {
        init_sgbd_ptr = postgres_init;
        connect_sgbd_ptr = postgres_connect;

        init_sgbd_ptr = mysql_init;
        connect_sgbd_ptr = mysql_connect;

This is basically what is happening here. Since PHP is VERY weakly typed, any variable can be either an integer, float, string, boolean, a function pointer. In fact, they are all of those types. It is when you use them that they fall into the intended one #quantumTyped.

Code of Conduct Report abuse