SitePoint Sponsor

User Tag List

View Poll Results: How do you specify public functions in PHP 5:

Voters
46. You may not vote on this poll
  • public function bar()

    36 78.26%
  • function bar()

    10 21.74%
  • Huh?

    0 0%
Results 1 to 24 of 24
  1. #1
    SitePoint Guru
    Join Date
    Nov 2002
    Posts
    841
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    To public or not to public

    Do you feel the public in the following definition is redundant in php 5:
    PHP Code:
    class Foo {
        public function 
    __construct() {}
        public function 
    bar() {}

    Do you leave it off or add it?

  2. #2
    SitePoint Addict
    Join Date
    Aug 2002
    Posts
    385
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i don't see it as being redundant

  3. #3
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are developing for PHP5 then it's only polite you actually use the full functionality no... There are other developers who will have to maintain your script once you're long gone

  4. #4
    SitePoint Zealot agoossens's Avatar
    Join Date
    Mar 2004
    Location
    Adelaide, Australia
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I add it all the time. It never hurts to be completely explicit with this sort of thing.
    This space for rent.

  5. #5
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I only specify protected or private. Seems redundant to re-specify the language default.
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  6. #6
    SitePoint Zealot Mau's Avatar
    Join Date
    Jan 2006
    Location
    California, USA
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think it might be required to have public there.

    EDIT: Just looked it up. I was wrong.
    Last edited by Mau; Mar 4, 2006 at 23:56.

  7. #7
    SitePoint Guru thr's Avatar
    Join Date
    Jun 2003
    Location
    Sweden
    Posts
    664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Explicit > Implicit for me. (I specify public)

  8. #8
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I leave it off.

    Anyone that doesn't know the default is public shouldn't be anywhere near your code.

  9. #9
    SitePoint Enthusiast siteartwork's Avatar
    Join Date
    Jan 2005
    Location
    Germany
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    public
    all the way, baby. And that's just because I'm so happy we finally have access levels in PHP5!

  10. #10
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > I leave it off.

    At the time, it didn't occur to me that a lot of people may well leave it out anyways, when they move from PHP4 to PHP5, since that is one reason PHP5s class methods were public by default, to sit well with BC, I assume anyways.

    Makes sense I suppose, but I didn't take much notice since I just dumped everything related to PHP4 and made a clean break

  11. #11
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always use it in new code, because I thought it was required according to E_STRICT standards.

    Looking at the manual again, both class variables and methods default to public; however, only failing to declare the visibility of a class variable will trigger the "Strict standards: Please use the public/private/protected modifiers" E_STRICT warning.

    I don't know, but that really doesn't make a whole lot of sense to me... default both to public for PHP 4 compatibility, but only warn about one under E_STRICT?

    I think I'll just keep on thinking what I thought before and always use a public/private/protected modifier. It sure makes things a bit more consistent.

  12. #12
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dreamscape
    I always use it in new code, because I thought it was required according to E_STRICT standards.

    Looking at the manual again, both class variables and methods default to public; however, only failing to declare the visibility of a class variable will trigger the "Strict standards: Please use the public/private/protected modifiers" E_STRICT warning.

    I don't know, but that really doesn't make a whole lot of sense to me... default both to public for PHP 4 compatibility, but only warn about one under E_STRICT?

    I think I'll just keep on thinking what I thought before and always use a public/private/protected modifier. It sure makes things a bit more consistent.
    The strict notice only warns, atm, when use "var $param", (IIRC) there was some mutterings about just making var a synonym for public, and removing the notice.

  13. #13
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ren
    there was some mutterings about just making var a synonym for public, and removing the notice.
    This is what I mean about consitency. In a language that at times can be so inconsistent, you sometimes have to make your own rules just to stay sane

  14. #14
    Non-Member Gator99's Avatar
    Join Date
    Sep 2004
    Location
    Florida
    Posts
    613
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ren
    I leave it off.

    Anyone that doesn't know the default is public shouldn't be anywhere near your code.
    In other languages, what some would consider to be "real" programming languages, the default is not public.

  15. #15
    SitePoint Guru
    Join Date
    Nov 2002
    Posts
    841
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So relying on the default generates a warning? Hmm, I'm not using E_STRICT. I'm thinking I should be. That sounds like a pretty good reason to always specify it.

  16. #16
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Selkirk
    So relying on the default generates a warning? Hmm, I'm not using E_STRICT. I'm thinking I should be. That sounds like a pretty good reason to always specify it.
    Doesn't generate a warning on methods, just properties.

  17. #17
    ********* Victim lastcraft's Avatar
    Join Date
    Apr 2003
    Location
    London
    Posts
    2,423
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi.

    Clutter.

    yours, Marcus
    Marcus Baker
    Testing: SimpleTest, Cgreen, Fakemail
    Other: Phemto dependency injector
    Books: PHP in Action, 97 things

  18. #18
    SitePoint Wizard
    Join Date
    Jan 2004
    Location
    3rd rock from the sun
    Posts
    1,005
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didnt vote because I am not sure what to do, and am even less sure now.

    Perhaps I will wait and see how its handled in the Zend Framework?

    :tongueincheeksmilie:
    isempty()

  19. #19
    SitePoint Addict
    Join Date
    May 2005
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think all languages should treat functions declared without visibility as public. You shouldn't have to be explicit...it's kind of silly.

  20. #20
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    Toronto
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think all languages should treat functions declared without visibility as public. You shouldn't have to be explicit...it's kind of silly.
    I typically prefer explicit because when I scan code, my brain expects to see something there. I must admit that I don't use it in interfaces methods since they are always public and when I think of it, there is no "non-static" counterpart to "static". So I certainly see the sense in your point.

    Regardless, I like things to be consistent and easily differentiated: when I don't see a method declaration without a visibility modifier attatched to it my brain immediately thinks function declaration instead of method declaration. It seems trivial but the few extra characters actually aid my productivity. (and one man's aid to productivity is another man's clutter -- go figure )

    Convention is a fine way to save time but explicitness removes that extra brain parsing step (and machine parsing, though that is moot in this case since both are recognized). I like to imagine that this is especially so for newcomers, though I'm in no position to know that.

    Another point for me is that PHP is hardly the only language I use. The less I have to remember in terms of its particular conventions and the more I can see explicitly stated, the better off I am. I'm getting old.

    (as a very minor clarification: all functions in PHP are always public -- of course you meant methods -- but that's the difference between relying on convention and requiring expliciteness )

  21. #21
    There is no general chat z0s0's Avatar
    Join Date
    Aug 1998
    Location
    Melbourne
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lastcraft
    Hi.

    Clutter.

    yours, Marcus

    Succinct ;-)
    Wormly Server Performance Monitoring
    Don't wait for an SMS at 4am. Find out what's really
    going on and fix the problem. www.wormly.com/website-monitoring

  22. #22
    SitePoint Addict
    Join Date
    May 2005
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jayboots
    I typically prefer explicit because when I scan code, my brain expects to see something there. I must admit that I don't use it in interfaces methods since they are always public and when I think of it, there is no "non-static" counterpart to "static". So I certainly see the sense in your point.

    Regardless, I like things to be consistent and easily differentiated: when I don't see a method declaration without a visibility modifier attatched to it my brain immediately thinks function declaration instead of method declaration. It seems trivial but the few extra characters actually aid my productivity. (and one man's aid to productivity is another man's clutter -- go figure )

    Convention is a fine way to save time but explicitness removes that extra brain parsing step (and machine parsing, though that is moot in this case since both are recognized). I like to imagine that this is especially so for newcomers, though I'm in no position to know that.

    Another point for me is that PHP is hardly the only language I use. The less I have to remember in terms of its particular conventions and the more I can see explicitly stated, the better off I am. I'm getting old.

    (as a very minor clarification: all functions in PHP are always public -- of course you meant methods -- but that's the difference between relying on convention and requiring expliciteness )

    You can call it a method if you like, last I checked it said...

    private function __construct()

    I view private & protected as modifiers in the same way that static or abstract are. You have a basic function:

    function DoSomething()

    which can be decorated with:

    [private|protected] [static] [abstract]

    each of which has a different effect on the nature of the routine.

    public doesn't actually *do* anything, so why bother?

    Yes, I suppose it's theoretically possible that one day the PHP internals guys will decide that the default visibility of all class functions (;-)) is private, but I trust them to have more common sense than that. Worrying about that is about as sensible as worrying that they're going to reverse the == and === operators or change the string concatenation operator.

    Beyond that, I don't really get the "language" difference things.

    php:

    class MyClass
    {
    function PublicFunc(){}
    private function PrivateFunc(){}
    }

    $Obj = new MyClass();
    $MyClass->PublicFunc();

    C++:

    class MyClass
    {
    public void PublicFunc(){}
    private void PrivateFunc(){}
    };

    MyClass Obj;
    Obj->PublicFunc();

    Java script:

    var MyClass = function()
    {
    function PrivateFunc(){}

    return {
    PublicFunc: function(){}
    };
    };

    MyClass.PublicFunc();

    Lets not even get into languages that don't have C-like grammar!

  23. #23
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    Toronto
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Last I checked, functions are refered to as methods just so as to denote that they indeed belong to a class just as class variables are traditionally refered to as members. Again, it was a trifle so I won't argue the point. Call it what you will.

    As I said earlier, I do appreciate your point in terms of the modifier principle -- and it is a good point. My point was that what makes something unambiguous to intention is a useful tool for me. In that light, my point about multiple languages was not a comparative one. It was meant to illuminate the idea that when jumping from one syntax/convention/style to another, the less that I have to internalize about how that model works, the better off I am. Your examples actually help drive that point home for me.

    Back to the point of intention -- if the visibility isn't specified explicitely you de facto rely on the notion that the default visibility was intended. So specifying "public" does do something -- it tells me that whoever wrote the function really did intend a public visibility as opposed to assuming that the lack of a modifier isn't an oversight.

    As an aside, I should note that if the PHP devs chose to keep the "var" keyword and instead allowed it to be modified (ie: [private|protected] var) then the consistency principle would make it much easier for me to internalize the dropping of the public keyword. This still wouldn't speak to intentionality but at least it would be less jarring (and it bears repeating) to me.

    Best.

  24. #24
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jayboots
    just as class variables are traditionally refered to as members.
    Actually, attributes. Class members are all of its attributes and methods together. Also, generally, properties are public attributes.

    That being said, there's not a general concensus on this or any other nomenclature; but this one seems most complete.


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
  •