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

alrazyreturn's avatar

I need help in design pattern

Hi , I create student ,teacher ,admin ,parent and is a part of user class I make name,email,authentication is common for all of these previous class , My question is : when I try to create new student for example I make the following 1- make a big validation for both user parameter and student parameter in student class 2- if it's valid I create new user and save it at student class 3- if it's save ok I create a new student is this design pattern is ok and if is there other better design kindly help

0 likes
11 replies
ayekoto's avatar

uhmmm,

I would do something like this:

roles: Id, name

users: role_id, id, username, email..etc

now roles will be diiferent users roles like: student, admin, teacher etc...

you shouldn't be creating different classes for users: I.e Student.php, Admin.php because actually they are all users with different roles

:)

alrazyreturn's avatar

many thanks dear, but the problem is each user student ,admin,teacher ,.. has different properties so for that I create the common attributes in user class and other unique attributes in it's specific class

ayekoto's avatar

@alrazyreturn , what do you mean have different user properties,?

remember there should be a profiles table to store users properties,

meanwhile you can use nullable in users or profiles table scheme on properties that are optional, that's if you decide to use one single profiles table for their properties.

but if you have use student, admin...etc as representing their different profile, then what's your challenge ?

1 like
WebKenth's avatar

I suppose you could structure your data model like @ayekoto says but also create an interface to interact with your desired users

Quick n' Dirty way would be to create ie. a Student model and point it's $table variable towards your users table. Then add a filter somewhere to ensure that you can't put a User with the Teacher role in it.

This would allow you to use a Student model to interact with your User model where their role is a student

1 like
alrazyreturn's avatar

many thanks for your replies ,but how I can make different validate with other optional fields , I mean I need to check level is inserted in student and I want job in teacher how I can validate in different cases ,sorry If my question is silly

ayekoto's avatar

@WebKenth, what will be the effect of Student model pointing to users table,

just wanna learn from what you trying to advice.

@alrazyreturn, if you checkout https://laravel.com/docs/5.4/validation#conditionally-adding-rules, you can add conditional validation either, one field should be required if the user is student,

or better still create a different view for different role, so u only show all fields they are to fill based on their roles

1 like
alrazyreturn's avatar

many thanks for both of you kindly if any one can give more help about that you are welcome :)

WebKenth's avatar

@ayekoto Well it's pretty dirty, and you don't really need a Model to represent a User's role, it's probably better to create a class which looks for a user Model of a given role

It would mostly be for sematic reasons, like when you update your User you can't see if you are talking about a Student or a Teacher, but if you had classes which represented users based on their roles, you could quickly differentiate them :)

1 like
alrazyreturn's avatar

Dear all, this is my code did I understand well or I missing something

class User { public $id; public $name; public $age; public $job_id; public $level; public $role_id;

function __construct()
{
    echo "welcome new user";

}


public function create()
{
    if($this->role_id==1)
    {
        echo "this is Student  user";
    }
    elseif($this->role_id==2)
    {
        echo "this is Teacher  user";
    }
    elseif($this->role_id==3)
    {
        echo "this is Parent user";
    }
    else
    {
        echo "this is parent user";
    }
}

}

class Student extends User { function __construct() { $this->role_id=1; }

}

$student= new Student(); $student->age=10; $student->level="ten";

$student->create();

Please or to participate in this conversation.