1. Interface in PHP is needed for interaction between the two classes?
  2. An abstract class is a class that implements at least one abstract method, and in what cases it is used and in which cases it is not needed?
  3. Is a static property available in all instances of classes?
  4. Does the static method work only with static properties and can be called without creating a new instance of the class?
  • I think it is better to divide this question into 4 - Mikhail Rebrov
  • Already figured out. - user254029
  • one
    Yes? .. well, then write the answer - someone else will need - Mikhail Rebrov

2 answers 2

  1. The interface is needed to declare a set of public properties and methods. And it can be used anywhere - for example, for passing parameters.
  2. An abstract class can be no methods at all. In addition, in a classical OOP, an abstract class should not implement anything at all, but only define for inheritance. It is used when it is necessary to use polymorphism for processing homogeneous objects. Not needed in the case of single functions or in the case of fixed, non-expandable structures.
  3. A static property belongs to the class itself, and, depending on the access modifier, can be used either only in the class itself (private), only in inheritors (protected) or from anywhere (public). Unlike dynamic properties, a static property is like one for all objects of a given class. Dynamic properties store values ​​for each object its own.
  4. A static method can use objects passed as parameters. But in principle - yes. A static method cannot have direct access to the dynamic properties of a class without an instance of this class.

    The interface is needed - to write the methods that need to be ALREADY redefined in the class heirs. Allows you to abstract from the code inside, and only indicates what should be laid in the class. Note:

    interface dataPicker{ public function getOneRow(); public function getAllRows(); public function getBestRow(); } 

    Now you can attach this interface, for example, to news, blogs, products, and they will be obliged to inherit these methods, and you will know that thanks to this interface you have such functionality.

    2) The abstract class may not have in itself an abstract method. Used when we have a class that acts as a template. Want an example? Look at the factory, there you can make 1 template, according to which 100500 new ones will be inherited. To understand how to use it, you need to feel ass, that class as a basis is good, but it's dumb to create its object.

    3) Yes, and it is the same for the whole class chain

    4) Yes, the thing is cool, used when it is seen that the class method is not tied in any way and does not need the logic of the constructor, example: $ user = User :: getUser (12);