Hi,
I'm new to php oop. In our system when creating a new user, user's role can be assigned by selecting from the role dropdown list.
Here, there are two classes, one is user class, another is role class. and user class use role class which is composition relationship.


I have two methods (getUserByUid($uid) and getRoleByRid($rid)), and It's not reasonable to put these methods in user class and role class. My question is where should put these methods?

Anyone can help me?

many thanks

The methods are simply functions within your class, so they go within the first-child set of your class
for example:

class myclass{
function __construct(){

}
function getUserbyUid($uid{
//code here
}
)
}

hope this helps :)

Perhaps you should read about extending classes :). Child classes implement all functions of their parent. So if you have for example child class getUserByRid, which is the child of getUserByUid, it would look like:

class getUserByUid
{
// code..
}
class getUserByRid extends getUserByUid
{
// this one contains all methods from getUserByUid. You can write new methods for this class as well, but getUserByUid won't be able to use them.
}

Is that what you mean?

Sorry no, I meant putting both as parents within the class (childs of the class)
And yes I do use extending classes (I teach programming) :D
Of course it all depends on how you want the code to run, I would need to see more code to work with it more

So you already both have them as child classes of a class? Then it would be impossible to derive methods from the one child class into the other, because in PHP each class can only have one parent class. For as far as I can see, you'll have to type all code twice, OR add it to their common parent class.

I hope I'm not completely missing the purpose of your question :p.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.