A
A
Alex Mirgorodskiy2018-04-05 20:37:32
PHP
Alex Mirgorodskiy, 2018-04-05 20:37:32

Why static methods in a class (PHP)?

Hello everyone) I immediately apologize for the probably stupid question, but still, can someone explain the thread when and when to declare static methods?
At once questions:
1. If you declare static that it is possible to address without creation of object Class:: statikFunk;
What does it give me?
Less load?
Just a convenience?
2. When to declare statics?
What is their juice so to speak? What problems are being solved.
I have already apologized for the stupidity of the question, please understand and forgive, and explain in a nutshell)))

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
ipokos, 2018-04-05
@AlexWeb6667

can be accessed without creating a Class::statikFunk object

Correctly.
the fact that you do not need to constantly initialize the class with its constructor.
those. no extra objects are created.
it is too..
For example, there is a helper that solves various small issues.
sort through the array somewhere, return the date somewhere, do something with the string somewhere
(i.e. these are simple tasks that do not require complex logic, connecting models, etc., but can be performed within the local area visibility of this method)
, such methods can be declared static.

D
Denis, 2018-04-05
@sidni

They are mainly used by helpers, such as a mini library for processing and / or receiving data,
for example, classes for working with strings, arrays, files, etc.,

Y
Yan-s, 2018-04-05
@Yan-s

To host related logic that needs to be available without creating an instance of the class.

G
Griboks, 2018-04-06
@Griboks

It's made for beauty. If suddenly it is more convenient for you to write DataManager::GetLastUpdateTime()instead of getLastDataUpdateTime().

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question