Why does Zend discourage “floating functions”?

Zend’s Coding Standard Naming Convention says

Functions in the global scope (a.k.a “floating functions”) are permitted but discouraged in most cases. Consider wrapping these functions in a static class.

The common wisdom in Python says practically the opposite:

Finally, use staticmethod sparingly! There are very few situations where static-methods are necessary in Python, and I’ve seen them used many times where a separate “top-level” function would have been clearer.

(Not only does the above StackOverflow answer warn against overuse of static methods, but more than one Python linter will warn the same.)

Is this something that can be generalized across programming languages, and if so, why does Python differ so from PHP? If it’s not something that can be generalized, what is the basis for one approach or the other, and is there a way to immediately recognize in a language whether you should prefer bare functions or static methods?

3

the basic difference between php global functions and their python counterpart is in their scope:

python top-level functions are part of the module where they are defined.
when they are imported, they are called with defining_module.function_name()
therefore there is less risk of conflicting names.

php ‘global’ functions are really global, iirc, no matter from where they are called. thus they are much more likely to create naming problems, e.g. be overwritten by some third-party library, etc.

3

In general, it’s a preference thing. “Static” in this context is just another word for “global”, so although either way will get you some evil looks (particularly if you’re only using classes as namespaces), it works.

But:

  • PHP can very easily be set up to autoload classes, making it so that static functions aren’t loaded in til you have need of them (or others in the same class). It can’t do the same for individual functions (yet), though. I’m not sure whether Python has an equivalent for either one, but i haven’t seen it yet.

  • Unlike global functions, static member functions can generally be made private, so it’s easier to expose only the bits you intend for outsiders to use. Well, except in Python, where you have to jump through some hoops to get private members.

  • PHP didn’t have namespace support til recently, meaning every top-level function was crammed into the same namespace. That’s changed, but PHP namespaces are still rather hideous. 😛

  • It seems Python doesn’t like making member functions static. Other languages have a keyword for that purpose; in Python, apparently you need an annotation (excuse me, “decorator”).

1

Python namespaces everything: Everything is in a module, and everything defined in a module has module scope rather than global scope (you have to go out of your way and do evil things to add something to “all” scopes). So name collisions aren’t an issue. In PHP, on the other hand, if you just go ahead and define a function, you put it in the same scope everyone is using for global names. You either explicitly use a namespace to prevent that, or you use a class as “namespace” (though the class should probably be in a namespace itself). The added bonus is that classes benefit from autoloaders, whereas functions do not IIRC.

There are philosophical differences too. The Python community is (and long has been) very open to utilizing multiple paradigms. Classes, higher-order functions and simple procedural programming are all equally accepted ways of doing stuff, as long as they fit the problem. And module-level data + functions does just what, so unless you need polymorphism or want a strong association with a particular class, there’s little benefit of attaching a non-method to a class.
In contrast, the PHP community (at least since the majority stopped screwing around and giving PHP the bad reputation it built up over the years) seems to be focused heavily on object-oriented programming. Free functions simply don’t fit into this paradigm, whereas static is widely accepted. I don’t quite get the logic behind that, but such is the convention.

Finally, note that staticmethod is not only eschewed because we don’t like the concept of static methods. Sometimes we do want static methods, but then we usually prefer classmethod, which does basically the same thing but gives more power to the method (it receives the class object, and can thus there can be polymorphism among classmethods). staticmethod is sometimes considered to be entirely redundant.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật