0

I have been making a website with PHP and I have a few classes, so far none of them really follow encapsulation rules of OOP. Lets say I have a user class like so:

class User{
    public $username;
    public $uid;
    public $date;
    public $location;
    public $bio;
    public $avatar;    
    public function __construct($user, $uidOr){
        $this->username = $user;
        $this->uidOr = $uidOr;
    } .

    public function getUser(){
        if($this->uidOr){
            $which = "uid";
        } else { $which = "username"; } 
// /\ I know the above is bad
        $getUserInfoQuery = new Database();
        $getUserInfoQuery->fetchAll("users", array($which, $this->username));

        foreach ($getUserInfoQuery->result as $value) {
            $this->username = $value['username'];
            $this->uid = $value['uid'];
            $this->date = $value['date'];
            $this->location = $value['location'];
            $this->bio = $value['bio'];
            $this->avatar = $value['avatarurl'];
        }
    }
}

That obviously is terrible practise of OOP, right? Should I be adding functions like getUserUsername(); which return the username of the user object or should I make a function which returns a table of information. What would be best? Thanks! PS: sorry for my bad code, im new to OOP.

2

Your getUser function would probably be better named populateFromDB, or maybe initFromDB. If the function name is getUser, I expect a User object to be returned. What happens here is some members are populated but as far as I can tell nothing is actually returned.

Having separate getters and setters for private members, where necessary, is a common practice. If you really don't think you'll ever need that you can instead have a single function named getUserFields which could return an array containing all of the different fields.

In your example, you've completely broken encapsulation by making everything public. So you might also want to make all the members private and then add whatever getter/setter functions are necessary (or a single function that returns everything in an array).

1

Your right that a big part of OOP is encapsulation. You run the risk of any code being able to modify your instance variables when you declare them all as public. Using public variables also encourages you to not encapsulate your business logic. Anyway, your not interested in why it's bad to declare public instance variables.

The simple answer to your question is yes it's bad practice to declare public variables. You should declare your variables as private and return them on an as needed basis. If you need access to a variable, then access it. If you want to return a bunch of information at once, then return the object containing the information. The whole point of that object is to contain the information you need. Returning all the info defeats the purpose of having an object.

It's common to use properties to represent instance variables in a domain object. A property can be read only or not based on its usage. Here's an example of your code written using properties.

class User {
    private String username;

    public String Username() {
        return username;
    }

    public void Username(String value) {
        username = value;
    }
}

If you want to make an instance variable read only, then omit the setter. If you don't want a variable to be readable at all, then don't include a getter and setter. People also refer to these are mutators and accessors.

Here's an example of PHP mutator and accessors being used. This link also provides PHP specific syntax that includes shortcuts. Shortcuts are nice because they clean up your code and they are universally understood.

http://www.refulz.com/mutator-and-accessor-methods-in-php/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.