0

I am currently developing a PHP application (v5.3.5 if it matters) and stumbled upon this question that I cannot seem to get a clear answer to while searching the internet.

So let's say I have a class Body that has two properties: m_bAlive and m_oHead. Head is an instance of a class that has setters and getters for it's properties (see example code). Should this object be public or private/protected as you'd normally declare it unless something else is necessary?

<?php
class Body {
    private $m_bAlive;
    private /*public*/ $m_oHead;
    //...
    //Getters and Setters here
}

class Head {
    private $m_bIsBald;
    //...
    //Getters and Setters here
}

By habit, I'd want to make $m_oHead private, but it feels as if it'd be overly complicated (and possibly slow?) to write $oBody->getHead()->getIsBald(). Maybe it just looks unusual to me because I don't come across this situation often, but I want to make sure.

Anpan
  • 1,146
  • 1
  • 10
  • 20
  • 1
    Have you come from a Java background by any chance? The m_ looks like something I saw a lot of in Java users but not very often elsewhere. Personally I would always go for having a getter and private or protected attributes, that was I can add additional logic if required at a later date(IE: what happens if in future you decide to make the head lazy load, meaning it might not exist when the object is first loaded). – scragar May 12 '14 at 16:28
  • It seem a duplicate question: http://stackoverflow.com/questions/419844/best-to-use-private-methods-or-protected-methods – Ivan Buttinoni May 12 '14 at 16:48
  • @scragar I personally didn't work with Java yet, but the head dev does and enforced this naming scheme. – Anpan May 13 '14 at 06:21

0 Answers0