PSR-12: Order of сonstants, methods and properties definitions

3,484 views
Skip to first unread message

Кирилл Фрейман

unread,
Jun 4, 2017, 9:19:38 AM6/4/17
to PHP Framework Interoperability Group
Hi everyone!

In PHP de-facto exists some best practice of ordering methods and properties. 
It seems to me that this order is most common:

Constants
    public const
    protected const
    private const
Properties
    public static properties
    public properties
    protected static properties
    protected properties
    private static properties
    private properties
Methods
    magic methods
    public static methods
    public methods
    protected static methods
    protected methods
    private static methods
    private methods

Should we standartize it? 

Alessandro Lai

unread,
Jun 5, 2017, 3:14:13 AM6/5/17
to PHP Framework Interoperability Group
I agree that a sequence of constants-properties-methods is ok and expected. But apart of requiring the constructor as the first declared method, I'm against a "MUST" keyword for the remaining stuff. We can maybe suggest and ordering of this kind, but for the most part the order shouldn't be this important, and I think also that the flow of execution between methods has some importance in the order between them; I think that also "Clean Code" suggests something like "keeping near a called method".

Andreas Möller

unread,
Jun 5, 2017, 7:06:02 AM6/5/17
to php...@googlegroups.com

Should we standartize it? 



Best regards,

Andreas

Alexander Makarov

unread,
Jun 8, 2017, 4:58:29 AM6/8/17
to PHP Framework Interoperability Group
Putting publicly visible properties before private ones is something new to me.

Stefano Torresi

unread,
Jun 8, 2017, 5:42:56 AM6/8/17
to PHP Framework Interoperability Group
I don't think this detail is worth standardising, ordering is very situational and can bear a lot of different semantics.
We can surely leave this to common user conventions.

--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+u...@googlegroups.com.
To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/211769db-61a6-4fb4-85fd-60c3508f78ac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Larry Garfield

unread,
Jun 8, 2017, 9:14:58 AM6/8/17
to php...@googlegroups.com
I am also fine with leaving this unspecified.  There's too many cases where I want to group things "logically", in a contextually-dependent way.

At best I could see "constants, then properties, then methods", but that's as far as I think it's reasonable to go and I'd be fine with not even doing that much.

--Larry Garfield

Woody Gilk

unread,
Jun 8, 2017, 9:34:04 AM6/8/17
to php...@googlegroups.com

I've taken to defining private properties at the end of the class, especially if the constructor is private.

This is not something that should be standardized, IMO.



For more options, visit https://groups.google.com/d/optout.
--

Cees-Jan Kiewiet

unread,
Jun 8, 2017, 11:08:00 AM6/8/17
to php...@googlegroups.com
Tbh I don't think we shouldn't specify class element order that deep. Different class designs call for different ordering IMHO.

To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+unsubscribe@googlegroups.com.

To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/211769db-61a6-4fb4-85fd-60c3508f78ac%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+unsubscribe@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+unsubscribe@googlegroups.com.

To post to this group, send email to php...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/php-fig/611d0c4a-6e1d-9b59-bc9d-78e9ebc9000c%40garfieldtech.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to php-fig+unsubscribe@googlegroups.com.

To post to this group, send email to php...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages