SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Arrow Why using Public is not advisable in OOP?

    Hello

    i already asked this before but i forgot the reason.

    Anyway why is it that using public is not a good habit in OOP?

    Can you give me reasons again please.

    Thanks in advance.

  2. #2
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    You mean using public on properties?
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,875
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You have to make some of the methods public or you will have no way to interact with the object.

    Making the properties private makes sure that they can only be updated via methods belonging to the object.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  4. #4
    Patience... bronze trophy solidcodes's Avatar
    Join Date
    Jul 2006
    Location
    Philippines
    Posts
    936
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Thanks stephen

    anyway i read this link below and it helps me alot.
    http://www.karlbunyan.co.uk/2004/12/...protected.aspx

    Thanks again guys.

  5. #5
    SitePoint Author Kevin Yank's Avatar
    Join Date
    Apr 2000
    Location
    Melbourne, Australia
    Posts
    2,571
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The public flag can be thought of as an advertisement to developers who would use this class in their own code. It says “This one’s for you! I’ve designed my class with the expectation that when you use it, you will call this method, or use this property!”

    If you used the public flag on every property and method, developers would have no way of knowing which parts of your class are there to be used and which parts are intended only for use by the class’s own code.

    So, while I wouldn’t say that using public is inadvisable, make sure that when you do use it, you’re doing it for the right reason: a public property or method advertises a piece of functionality that is intended to be invoked by code outside of the class.
    Kevin Yank
    CTO, sitepoint.com
    I wrote: Simply JavaScript | BYO PHP/MySQL | Tech Times | Editize
    Baby’s got back—a hard back, that is: The Ultimate CSS Reference

  6. #6
    Floridiot joebert's Avatar
    Join Date
    Mar 2004
    Location
    Kenneth City, FL
    Posts
    823
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like your explaination Kevin Yank.

    I used to wonder why anyone would want to declare anything other than public when I first tried to wrap my head around visibility. Then I got into iterators and other things that use visibility to determine how they work and the other options started to make more sense.

  7. #7
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    An object many times needs the ability to delegate, abstract or otherwise manipulate incoming data. Making all properties private or protected assures all instances of a class maintain this control. Ultimately making code easier to follow and debug because the state can only be altered through the public interface. When writing application code I never allow explicit public access to the objects state for these reasons.

  8. #8
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, as the writer of the class, you are free to change how the class works internally without breaking other people's code, because you set all your internal stuff private, so you can do anything you need with them.

  9. #9
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Example:
    PHP Code:
    <?php
    Class SomeClass{
        public 
    $Database//needs to be PDO object
    }
    Class 
    SomeClassMarkII{
        protected 
    $Database;
        public function 
    setDatabase(PDO $Database){ $this->Database $Database; }
        public function 
    getDatabase(){ return $this->Database; }
    }
    $SomeClass = new SomeClass;
    $SomeClass->Database 0;
    $SomeClassMarkII = new SomeClassMarkII;
    $SomeClassMarkII->setDatabase(0); //exception - must pass a PDO object
    ?>
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •