2

I'm doing this software in PHP for my final school work and i must use OOP. I have this class "user" and it doesn't have any attributes in it, just methods(things the user can do like: register, post and background actions like encrypting it's password). I want to know if this is ok or not, the code works and all but i want to make sure i'm using OOP in PHP well. My class looks like this:

class user{
    function cryptPass($input){code}
    function regist_user($name,$email,$gender,$dob,$password){code}
    --more methods--
  }

Should i keep doing this or is it mandatory to have attributes up there for the user?

Gásten
  • 123
  • 3
  • 8
  • 2
    This usually indicates you don't need an object to do this and may be over complicating your code – John Conde Dec 01 '13 at 15:48
  • My goal here was to put everything that my user can do in one place so i just call it whenever the user needs to perform the action. This way i won't have a lot of php code on every page, it's all in one place and well organized. I think that will save some time when debugging. – Gásten Dec 01 '13 at 15:52
  • You shouldn't need classes to organize unrelated pieces of code. Good namspacing, etc, should do that for you. – John Conde Dec 01 '13 at 15:55
  • You can use class without any attributes if you don't need any. – The Alpha Dec 01 '13 at 15:59
  • I haven't used namespacing yet.I think the code is related, isn't it? – Gásten Dec 01 '13 at 16:00
  • This was worrying me because i'm doing a class diagram for the first time and i don't have any attributes to put there. – Gásten Dec 01 '13 at 16:02

2 Answers2

2

This isn't really a class if it doesn't have any attributes. You're using a class just to group a bunch of functions together. A class is used as a data structure to represents objects, like a real user with a name, a password, etc. Here, you just have utilities function which store nothing in memory.

A real user class should be like in this thread.

What you need is to use namespaces, to organize your application.

Community
  • 1
  • 1
Maxime Lorant
  • 34,607
  • 19
  • 87
  • 97
  • Yes, that's exactly what i need to do. I'll take these:$name,$email,$gender,$dob,$password and define them as attributes and change their values when the user registers using the regist_user() function. Right? – Gásten Dec 01 '13 at 16:11
  • 1
    You doesn't need to make a project full OOP, it's heavier and not practical. – Maxime Lorant Dec 01 '13 at 16:12
  • 1
    So do it if it's required, but it's not the best solution. It's syntactically correct but it's not the philosophy of the OOP. – Maxime Lorant Dec 01 '13 at 16:17
  • I hope i use it better on other objects i have. I only have 2 weeks left so i don't want to get into a lot of details but since i'll still use the software after all this, i'll surely review the code. – Gásten Dec 01 '13 at 16:23
1

First you decide what type of object it is going to be. (State-full or Stateless)

State full objects supposed to have attributes to represents the state. But stateless objects can have only thread safe subroutines which performs some operations based on given parameters.

Balaji Reddy
  • 5,576
  • 3
  • 36
  • 47