metalzade
metalzade

Reputation: 35

Class methods and SOLID principles in PHP

Let's imagine we have a Class called Register, This class only Registers user and shows a message if validation doesn't pass.

 class Register {

        function register_user() {

            // Do Validation, Get message and Show it if faild
            ...
            $message = $this->get_message( $error_code );
            ...


            // Register User Codes
            ...

        }


        // Some other related methods
        ...


        function get_message( $error_code ) {

            // Returns **different messages** based on error code.

        }

  }

My question is this: Should get message be a part of Register class, Or should it have its own class for handling messages for Register, Login, ForgotPassword... ?

Which one is more SOLID friendly?

Upvotes: 0

Views: 243

Answers (1)

In a good OOP implementation you would create a user class with functions (methods) like register, login, logout, display_user, modify_user, and more functions. Your object is the user. Having a class with only a register_user function, another class with only a login_user function, would complicate your code for no reasons and goes against basic OOP. I don't know much about SOLID but you can certainly make individual functions safe and robust by using modifiers like private, protected, final and more.

Upvotes: 2

Related Questions