Y
Y
Yan Aleksandrov2021-02-02 11:20:45
PHP
Yan Aleksandrov, 2021-02-02 11:20:45

Calling a non-existent method?

In OOP, you need to implement data acquisition for a number of predefined tables from the database. Is there a better solution than __callStatic?

An example of what's available so far:

class Field {
    public static function __callStatic( string $name, array $args ) {
        // здесь MySQL запрос типа (для иллюстрации поведения)
        return SELECT * FROM {$name}fields WHERE id = $args['0'] AND name = $args['1']
    }
}

And getting data: Sadness is that only 2 arguments can be passed to the __callStatic method. And the second only in the form of an array. Is there a pattern that would allow this to be implemented in a view? I tried this: But the interpreter returned an error as expected. Since you can only transfer an array.

echo Field::post( [ 2, 'meta_field' ] );

echo Field::post( 2, 'meta_field' );
echo Field::post( 2 )->meta_field;

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2021-02-02
@YanAlexandrov

You don't need to pass anything "as an array", the php engine will do it by itself if it doesn't find an existing method.
https://ideone.com/EsnAEZ
EtT1rUn.png

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question