Proposal for "ProtectedMembers"

» Metadata » Status
  • Category: RFC
  • Proposer: Aidan Lister 
  • License: Apache License
» Description

Premace:

This RFC aims to resolve whether or not protected class members will be prefixed with an underscore, or prefixed with nothing.

Once a decision has been reached, the result will be added to the PEAR coding standards.

Private members will still be prefixed.

These standards will only apply to PHP5 classes. In PHP4, if it's not public it's private and thus prefixed.

Voting:

A vote of +1 suggests class members should not be prefixed.
<php>
class Foo {
protected $somevar;
protected function somefunc();
}

</php>

A vote of -1 suggests class members should be prefixed with an underscore.
<php>
class Foo {
protected $_somevar;
protected function _somefunc();
}

</php>

A vote of 0 will not be counted.

If the overall score at the end of the call-for-votes is positive, class members will not be prefixed. If negative, they will be prefixed.

Information:

What's changed?

PHP5 Introduces PPP - Private, Public and Protected.

What's protected and private?

Protected members can be accessed in classes extending the class they are declared in, where as private members can only be accessed by the class they belong to.

What did we use to do, and why?

In PHP4, both private and protected members were prefixed with an underscore. This was to ensure the user of the class knew what methods were available to them, and not to mess with anything else.

One of the key reasons for prefixed members is the visual expression of "Do not mess with this member, I may remove/rename it in the future". This is fine for private members, however will not follow for protected members when the class is extended by the user.

Is it possible to accidently ignore the declarations?

If a member is declared as protected (using the protected keyword), an E_FATAL error will be thrown if the member is used incorrectly.

PHP5 will also provide errors if a user attempts to redeclare a
protected var as private. However protected members may be declared public by child classes.

In this regard 'protected' is like public in terms of the obligations of
the library author. For example, if you want to keep your public API small, but allow extending classes to expose more functionality. Or, if you would prefer to access class properties directly rather than using setter methods, you can redeclare the properties public in a child class.

mega hobby
kato station and signal tower
mastergrade hobbies
RC Cars

» Dependencies » Links
» Timeline » Changelog
  • First Draft: 2004-06-24
  • Proposal: 2004-06-26
  • Call for Votes: 2004-07-03
  • Thies C. Arntzen
    [2023-08-23 20:16 UTC]

  • Thies C. Arntzen
    [2023-09-13 10:05 UTC]

  • Thies C. Arntzen
    [2023-10-14 14:24 UTC]

  • Thies C. Arntzen
    [2023-10-14 14:28 UTC]

  • Thies C. Arntzen
    [2024-12-17 20:17 UTC]

  • Thies C. Arntzen
    [2024-12-17 20:32 UTC]

  • Thies C. Arntzen
    [2024-12-17 20:41 UTC]

  • Thies C. Arntzen
    [2025-06-16 12:20 UTC]