-1

Hello,

I want to class name are very specific. Like: form.class.php or database.class.php and i access them via autoload.

But sometimes i need a function which not specific.

For example: get avatar for current user or relation between two user.

I am using function.class.php for these function but function.class.php is growing up.

How can i do ?

Note that: English is not my native languages.

Best regards.

Kerem
  • 37
  • 7
  • 2
    Hi Kerem. I'm not sure if it was you who was responsible for me recent skyrocket in upvotes, but usually it's enough thanks to upvote answers on your own question(s) (and accepting them ;)). Not sure if it follows SO guidelines... still... Thanks! :) – Bono Mar 03 '15 at 21:36
  • @Bono woah. Those will probably all get rolled back tomorrow, but if not... lol cha-ching! – Chris Baker Mar 03 '15 at 21:43
  • Thank you for your kindly answer. And you @ChrisBaker your rep is awsome. By the way your link very good. I have read it. – Kerem Mar 04 '15 at 13:07

2 Answers2

2

If you have users I assume you have a User class. If you don't you should consider making one. In this user class you can store all your User methods, like:

class User{
    private $avatar;    

    public function getAvatar(){
        return $this->avatar;
    }

    public function setAvatar($avatar){
        $this->avatar = $avatar;
    }

    //etc.
}

You would use them like this:

$user->getAvatar()
$user->getRelation($other_user);

Edit
Thanks to Chris Baker for additions in the comments. He specified this article for people starting out with OOP. I also recommend to take a look at it!

Bono
  • 4,757
  • 6
  • 48
  • 77
  • 1
    ... I'm not so sure about this advice. You wind up with giant god-classes that are not objects, but rather a pseudo-namespace for a ton of unrelated functions. In SOLID principles, an object should only have one responsibility. How do you easily substitute a User class with 100 methods? How are you managing the dependencies that these functions all have? This is not good practice. – Chris Baker Mar 03 '15 at 21:18
  • I have never seen the need myself to raise these classes to a 100 methods. I usually use these simple classes for just getters and setters and have more complicated functions splitted up in classes that are better suited for the job (database insertions and the like). I think for now this will get OP on his way. So far this class only has one responsibility and that is handling user data. I agree with you though, it's definitely not smart to have huge classes (god classes). But so far I think OP is not yet at the level that this becomes a concern. I'll upvote your comment for OP to look at :) – Bono Mar 03 '15 at 21:21
  • @ChrisBaker if you don't mind I would like to see an answer of yourself. You seem to be quite in the know, and I'm always looking to know more. I for one am not known (very well) with SOLID. I'll have a look at it in the mean time (maybe I've been applying it without knowing it's called that... or maybe not ;)). Cheers – Bono Mar 03 '15 at 21:25
  • 1
    I don't believe there is enough information about the structure of the project in this question to provide a useful suggestion on which pattern(s) he ought to use. I agree that these concepts are currently beyond the OP's range of knowledge, but if he's ready to start writing OOP code then now is the time to start learning the best practices before he gets bad habits. Start with some articles about OOP, like this one: https://nikic.github.io/2011/12/27/Dont-be-STUPID-GRASP-SOLID.html -- don't take offense at the name, no one's insulting anyone --- all abbreviations :) – Chris Baker Mar 03 '15 at 21:41
  • @ChrisBaker Thanks, I'll add it in my answer for (future) readers. – Bono Mar 03 '15 at 21:44
  • I suspect this question will be closed, but if you want to put in the effort at guessing what advice to give the OP, I won't mock your effort :) – Chris Baker Mar 03 '15 at 21:46
1

In object-oriented programming, a class is a template that defines the state and behavior common to objects of a certain kind. A class can be defined in terms of other classes. For example, a truck and a racing car are both examples of a car. Another example is a letter and a digit being both a single character that can be drawn on the screen. In the latter example, the following terminology is used:

The letter class is a subclass of the character class; (alternative names: child class and derived class) The character class is immediate superclass (or parent class) of the letter class; The letter class extends the character class. The third formulation expresses that a subclass inherits state (instance variables) and behavior (methods) from its superclass(es). Letters and digits share the state (name, font, size, position) and behavior (draw, resize, ...) defined for single characters.

Source: https://staff.fnwi.uva.nl/a.j.p.heck/Courses/JAVAcourse/ch3/s1.html

Bono
  • 4,757
  • 6
  • 48
  • 77
EngineerCoder
  • 1,445
  • 15
  • 30