It requires solving a problem that is very easy to implement in PHP, but it is unclear how - in Java (talking about logic). I need to create an instance of one of the 2 objects for later access to its properties / methods. What kind of object - it depends on. conditions For Java, a similar solution:

if("Заказчик".equals(uStatus)){ Customers UserObj = new Customers(); }else{ if("Диспетчер".equals(uStatus)){ Bosses UserObj = new Bosses(); } } Content = UserObj.getForm(); 

... does not work, because outside the condition, it does not see the UserObj object. Execute each code variant within all blocks depending on the condition:

 if("Заказчик".equals(uStatus)){ Customers UserObj = new Customers(); Content = UserObj.getForm(); }else{ if("Диспетчер".equals(uStatus)){ Bosses UserObj = new Bosses(); Content = UserObj.getForm(); } } 

... hardly rational. What could be the right solution in terms of the concept of Java? I understand that for those who write not her, the question is elementary. I would be very grateful for the clarification.

  • @falstaf - It seems to work, thanks! However, another theoretical question: When I simply declare a variable of the type of the parent class: Handlers hdl; it shouts to me that the variable is not initialized, although further (inside the condition) I save an instance of one of the objects (Boss / Customer) in it. If I write, for example, Handlers hdl = null; then it says everything is OK. So why does it see an error in the first case? - srgg67 pm
  • one
    Is it a compiler? Look at your ifs. If uStatus is not equal to "Customer" or "Manager" - then UserObj value is not initialized. - alexlz
  • Thank you all! It is unbelievable how the solution of such an elementary task can help to see really important things. And, I admit, @DreamChild made a valuable comment about OOP. - srgg67

1 answer 1

  1. Bring the getForm method into the interface, implement this interface with the Customers and Bosses classes.
  2. Before starting the if / else block, declare a variable of the type declared above.
  3. Depending on some conditions, assign this or that interface implementation to this variable.
  4. After exiting the if / else block, call the getForm interface method and get the result.
  • Hmm, really, the solution is just brutal. More simply it is impossible to think up implementation? To fulfill the simplest condition, construct an entire interface - is it really an integral part of the concept ?! - srgg67
  • one
    You can do without an interface if Customers or Bosses are interconnected by inheritance and the getForm method is declared in the base class. - falstaf
  • one
    > Hmm, really, the decision is just brutal sorry, but what do you dislike? They gave you very good advice - qualified developers do just that. But these your branching if-elses with a binding of logic to string (!) Values ​​inside the method are a silent horror, not otherwise. The fact that PHP with the help of some of its regular crutches allows you to bypass this - nothing more than a feature of a single php. In more normal languages, the scope of a variable is more demarcated. So than to criticize, it is better to take note - DreamChild
  • one
    Imagine that there is no getForm() method for the object you are returning - what then? The code with the interface ensures that the method is and will be executed. The php code does not guarantee anything, hence the well-deserved fame of php as a language not demanding to the quality of the code. - VladD
  • one
    You surprise me. It seems to reason reasonably, but for some reason you don’t understand simple things (don’t think it offensive) The meaning of interfaces in an OOP is to isolate similar behavior from different entities in which this behavior is sometimes realized in completely different ways so that these different entities use, regardless of their differences. As for the momentary task - well, you know, the PLO is not for the solution of momentary tasks. And in them its use may be redundant - DreamChild