A
A
Alexander2016-08-09 15:42:09
PHP
Alexander, 2016-08-09 15:42:09

Where is the line between instantiating an object (new Class()) and using the scope resolution operator (::)?

I read a lot of material, but did not understand (maybe dumb). In what cases should an object be instantiated, and in what cases the visibility permission :: is enough.
Please explain using the form validation class as an example:
Now I am doing this.

$data = Request::post('text');
// Создать экземпляр формы.
$form = new Form($rules);
$form->validate($data)
if(!$form->isValid()) {
  echo $form->error()->msg();
}

Is it necessary to create an instance? Or you can do with static methods:
$checkForm = Form::validate($rules, $data)
if(!$checkForm) {
  echo Form::getError();
}

The only thing I know is that an instance must be created when it operates with unique data (hello PDO)
Tell me. Where is this very line between new and ::
When is it necessary and when is it not necessary?
(Most likely the answer is on the network, but I can not formulate a request)

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexey Ukolov, 2016-08-09
@websiteserf

Static methods should be used as named constructors. For everything else, it's better to create instances of objects, because static methods have nothing to do with OOP, by and large, and therefore blur the paradigm for those who are not very well versed in it.
If there is no work with state in a static method and there are many such methods, most likely the author of the code is not very imbued with the ideas of OOP and the code is written in a procedural style. And if there is a modification of some state, then this is work with global variables (why global variables are bad, I hope, there is no need to explain).
You have this in your code echo Form::getError();: Where will the static method get information about the form error from? How will he distinguish the errors of one form from another?

The only thing I know is that an instance must be created when it operates with unique data
An instance is needed when it has some kind of its own state (validation errors, for example). Two objects may have the same state (that is, be non-unique), but this does not mean that one of them will not change in the next moment.

T
trevoga_su, 2016-08-09
@trevoga_su

you don't know how to think with objects yet,
so you ask these questions. and you get even more stupid answers.
read at least the first half-books of Grady Butch

A
Alexander Aksentiev, 2016-08-09
@Sanasol

There is no significant difference in functionality except that $this cannot be used by a static call.
As it is more convenient in general...
And yes(@alexey-m-ukolov) it's not OOPish.

D
Dmitry, 2016-08-09
@Sad_Bro

it is convenient to store several methods of similar purpose in such a class, use the class as a container, without inheriting anything from it, but simply using it as a container. For example Math::rand() or Math::sqrt($a); something like that

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question