SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Mar 2008
    Location
    Dublin, Ireland.
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calling a private property from outside a class.

    Hello all,

    I've new to oop and I'm stumped as to why this piece of code isn't returning a value. I know that the property $legs is private but I thought that by using the constructor to include the numLegs method that this would enable the object $table to call the $legs property. But I just get "cannot access protected property" and I can't understand why. Can anyone point out where I'm going wrong please?

    class Table
    {
    private $legs;

    function __construct()
    {
    $this->numLegs();
    }

    public function numLegs()
    {
    $this->legs = 4;
    }
    }


    $table = new Table();
    echo $table->legs;

    Thank you very much.

    derm_w

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code within the Table class is free to use it's own private variables however it wants(reading, or assigning values to them).

    But this:
    PHP Code:
    echo $table->legs
    Is not code within the Table class. This is separate code which is making use of a Table object.

    You probably want to make a public method to access the value
    PHP Code:
    public function getNumLegs()
    {
    return 
    $this->legs;


  3. #3
    SitePoint Enthusiast
    Join Date
    Mar 2008
    Location
    Dublin, Ireland.
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, thank you crmalibu for making that clear, I see it now.

    It really takes time before the oop concepts begin to sink in.

    Kind regards,

    derm_w

  4. #4
    SitePoint Guru
    Join Date
    Nov 2002
    Posts
    841
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I like to think of the visibility declarations as forming an onion or a pearl (or a pearl onion). Each class provides a contract or an interface to the outside world. The visibility declarations define and enforce this interface.

    By using private, you are declaring the innermost part. This is the grain of sand that forms your pear and the center of your onion. Only the current class can access this code. These are the internals of the class.

    When you declare something protected, you declare an contract with other classes that might inherit from the current class. You are controlling how descendant classes can interact with their parent classes. This forms a conception interface that can be accessed via inheritance. (Not to be confused with the explicit interface construct and keyword.)

    When you declare something public, you are also declaring an implicit interface consisting of features that other classes in the same program can use.

    I know some people tend to use private as their default visibility. I almost always use protected unless I'm going to consider the class "published."

    more on Published vs. Public(pdf)

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2008
    Location
    Dublin, Ireland.
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much Selkirk for your nice onion imagery which I find very helpful.

    I think it's by these different perspectives on concepts that I find difficult to grasp that a sounder understanding begins to take hold.

    Kind regards,

    derm_w


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
  •