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

TOP OF THREAD FULL DISCUSSION
re: 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,...
 

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) {
    case DBMS_TYPE_POSTRESQL:
        init_sgbd_ptr = postgres_init;
        connect_sgbd_ptr = postgres_connect;
        break;

    case DBMS_TYPE_MYSQL:
        init_sgbd_ptr = mysql_init;
        connect_sgbd_ptr = mysql_connect;
        break;
    }
}

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