Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vsjoshi's avatar

Type Hinting from Variable

Hi all,

I am taking a pretty dynamic approach, so it might not be very easy to understand...

What we want to achieve is to get Class name and Validator from the database. But that causes me a problem. Given the following method in a "generic" class, I want to call the "update" method you see at the bottom, in the class which name I fetch from database. So far it works, but my Problem is now with the custom Request class. I also fetch the Request class (validator) name from database, and I think I need to return it as first parameter to the update method here, but I am not sure how to do that, because to the Update function here I can only pass a generic Request, but then I want to switch from this generic Request to a, for example, SaveItFormRequest. can I convert a Request to a SaveItFormRequest? or how can I pass an instance of SaveItFormRequest with my current input data to the update method in the dynamic class?

public function update(Request $req, $answerId) {
            //dd($req->all());
            $formId = ItRequests::whereId($answerId)->first()->form_id;
            $formData = Forms::whereId($formId)->select('form_answer_table_name', 'validator_class_name')->first();
            $className = convertSnakeCaseToCamelCase($formData->form_answer_table_name);
            $controller = __NAMESPACE__ . '\\' . $className . 'Controller';
            $request = 'App\\Http\\Requests\\' . $formData->validator_class_name;
            $ret = (new $controller())->update($request, $answerId); // PROBLEM HERE
        }
0 likes
0 replies

Please or to participate in this conversation.