Encapsulation: Separate functions for separate variables, or return a table? [PHP]

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.

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).

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/

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Encapsulation: Separate functions for separate variables, or return a table? [PHP]

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.

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).

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/

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật