i have written code using object manager in Block. i am not sure it is best practice. what are the place use object manager. what are the place we need to avoid Any one suggest please
- In a Factory or Proxy class, to create object instances.
- In a class
__constructfunction, to preserve backward compatibility for an extension you publish. (Don't do this if it's private code for a particular site; you don't need to preserve backwards compatibility.)
- In formal tests.
Do not use ObjectManager:
- In a template. (Use DI on that template's block.)
- In a class method. (Use DI in that class's constructor.)
- Anywhere else.
- Out of laziness.
- Because it's raining out.
- Because someone on the internet said you should. Unless your situation is on the list above, they were probably mistaken.
Why? / see also
You should not use the ObjectManager directly!
Exception from the rule are:
- in static magic methods like
- in case you should make backward compatibility of constructor
- in global scope, like in fixtures of integration test.
- in class that needs only for creation of object like factory, proxy , etc