OO PHP static keyword, should I use it?

I’m writing script for fb and I have 3 objects that I’ll be using through all classes. I’m wondering if there is any advantage in using the static keyword except I don’t have to create an instance every time I need to use a method/object? Which way should I go, with static or without it?

I know this is basic, but I’m new to OOP and I want to learn to use it in a proper manner.

1

Your question is a little vague so I’ll try my best.

There’s one good question to ask when you’re trying to choose between creating a static or an instance method.

Do you need to persistent state between method calls?

Static methods are no different than traditional procedural functions. You give it an input and it should consistently product an output based on the input that is completely side-effect free.

Basically, you could use a traditional function declaration in it’s place with no difference. The only benefit is that static methods are attached to a namespace where traditional functions are defined in the global namespace.

They don’t matter so much in PHP because the language mixes both procedural and OOP styles but in languages that are pure OOP (ex C#/Java) there is no way to define functions so static methods are used in their place.

Static methods are perfectly acceptable to use when you’re defining helper methods that act in a stateless manner. If you need data persistence between calls and/or find yourself relying on global variables, don’t; use objects/instances instead.

Update:

Tom B made a very good point about unit testing. That static methods (like procedural functions) are inherently decoupled from the classes that they’re attached to. So, if a static method fails, it may also cause any other instance method it’s called in to also fail. It’s a classic example of a side-effect.

That explains why I have encountered so many people parroting ‘static methods are bad, mmmkay’ in the TDD community. They have a completely valid point.

There are ways this could be improved. In the language, throwing an implicit exception when static methods fail. Technically, it wouldn’t be any messier than doing type conversions. Assume it’s going to break something if it fails. Of course, that assumes a higher degree of understanding to implement.

Either way, it comes down to the simple question.

Do you limit the language you use, or limit the ability to test your code’s consistency?

My personal bias is toward expressiveness, the TDD school of thought’s bias is toward consistency. YMMV.

I look forward to many years of religious wars over this topic.

1

In short: No. Static methods are generally considered bad practice. They introduce a lot of potential issues. They are a way of shoehorning procedural code into OOP.

  1. They introduce hidden dependencies. Code which arbitrarily calls foo::bar() has a dependency on foo and cannot run without foo being defined. In this instance, the object using foo::bar() will construct correctly but won’t be usable if foo is not defined. It will only break at the point foo::bar() is called. If a $foo instance was passed into the object’s constructor then the object could never even construct without it’s dependencies. This creates far more robust code.

  2. Statics are globals. Global state is very bad, anything can change the code and its state is unknown. You sacrifice the power and control achieved by OOP encapsulation by using static methods.

  3. It’s impossible to substitute the functions for a different version. In the foo::bar() example, you sacrifice all the power polymorphism enables you in OOP by using static methods.

  4. It makes unit testing impossible. For unit testing, you need to be able to substitute dependencies for mocks. Using static methods makes this far more difficult.

  5. Are you sure you only want a single instance? Imagine if you had a class that created an FTP connection and allowed a simple API for creating files. Just calling FTP::upload($file); would be quite clean wouldn’t it? The problem is, once the requirements change to “can the file now be uploaded to the backup server as well.” then you have a problem: there’s no way to connect to two different servers at once.

Static methods should be avoided as a matter of course. From an OOP perspective, there is no reason they should ever be chosen over an object with a method.

9

I’m a bit confused by your question.
You have three objects: Instantiate them and pass them around by reference. You need three instances, use three instances, assuming that they are not modified do not need to be cloned etc. Alternatively check singleton pattern.

Static keyword: https://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-java. It is exactly the same in PHP as in JAva snd elsewhere: you need it specific for the class, then it’s static. Why has to do with maintenance, performance and other factors.

New to OOP: takes time, do not worry.

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