Home php Using empty interfaces to combine object types

Using empty interfaces to combine object types

Author

Date

Category

I sketched the schematic class Logger , and two classes, readexception and writeException , which extend the Exception class. In the client code, respectively, there is a block for making exceptions. In this embodiment, it turns out that if I add another class (for example FooException ), then in the try {} catch () {} {} I need to add another block Catch (FooException $ E) {} . Here is pseudocode:

class readexception extensions exception
{
  Public Function __Construct ()
  {
    Parent :: __ Construct ('' file read error! ');
  }
}
Class WriteException Extends Exception
{
  Public Function __Construct ()
  {
    Parent :: __ Construct ('Error writing to the file!');
  }
}
Class Logger.
{
  / **
   * @return Logger.
   * @Throws readexception
   * /
  Public Function readlines (): Logger
  {
    if (true) {// False | TRUE to switch exceptions
      Throw New Readexception ();
    }
    Return $ this;
  }
  / **
   * @Throws WriteException
   * /
  Public Function Writelines (): void
  {
    if (false) {// False | TRUE to switch exception
      Throw New WriteException ();
    }
  }
}
Try {
  $ Logger = New Logger ();
  $ Logger- & gt; readlines () - & gt; writeLines ();
} Catch (readexception $ e) {
  Echo $ E- & GT; GetMessage ();
} Catch (WriteException $ E) {
  Echo $ E- & GT; GetMessage ();
} // and here it can be how many cached blocks () {}

I thought of using an empty Exceptions interface, and this allowed you to burn only one Catch unit:

try {
  $ Logger = New Logger ();
  $ Logger- & gt; readlines () - & gt; writeLines ();
} Catch (Exceptions $ E) {
  Echo $ E- & GT; GetMessage ();
}

Here is an interface option:

interface exceptions {}
Class ReadException Extends Exception Implements Exceptions
{
  Public Function __Construct ()
  {
    Parent :: __ Construct ('' file read error! ');
  }
}
Class WriteException Extends Exception Implements Exceptions
{
  Public Function __Construct ()
  {
    Parent :: __ Construct ('Error writing to the file!');
  }
}
Class Logger.
{
  / **
   * @return Logger.
   * @Throws readexception
   * /
  Public Function readlines (): Logger
  {
    if (true) {// False | TRUE to switch exceptions
      Throw New Readexception ();
    }
    Return $ this;
  }
  / **
   * @Throws WriteException
   * /
  Public Function Writelines (): void
  {
    if (false) {// False | TRUE to switch exception
      Throw New WriteException ();
    }
  }
}
Try {
  $ Logger = New Logger ();
  $ Logger- & gt; readlines () - & gt; writeLines ();
} Catch (Exceptions $ E) {
  Echo $ E- & GT; GetMessage ();
}

The interface option allows you to create as many classes that extend the exception, and you do not need to add anything in the client code.

Essence of the question: Is it possible to use empty interfaces only to combine object types? Is it a normal practice?


Answer 1, Authority 100%

In PHP there is a basic interface Throwable that can be used as The basis, if it does not differ in functionality from your.


Answer 2

Use as needed. I will give an abstract example:

// In this example, empty interfaces will serve as something similar
// on the "attribute without meaning" or "property", the presence of which indicates
// on the possession of PHP or JavaScript technology (specifically for this).
// Create 2 interfaces (after carefully read the comment above)
Interface PHP {}
Interface JavaScript {} 
// create an abstract class and developers add a method to produce summary
abstract class Developer
{
  protected $ resume = '';
  public function getResume (): string
  {
    return $ this- & gt; resume.PHP_EOL;
  }
}
// backend developer implements an interface Php, which means possession PHP technology.
class BackendDeveloper extends Developer implements Php
{
  protected $ resume = 'I am Backend developer PHP';
}
// frontend developer implements an interface Javascript, which means ownership of Javascript technology.
class FrontendDeveloper extends Developer implements Javascript
{
  protected $ resume = 'I am Frontend developer JAVASCRIPT';
}
// fulstak developers to implement interfaces Php and Javascript, which means the ownership of these technologies.
class FullstackDeveloper extends Developer implements Php, Javascript
{
  protected $ resume = 'I am Fullstack developer PHP and JAVASCRIPT';
}
// novice developer does not implement or Php Javascript interface, it does not own the technology.
class JuniorDeveloper extends Developer
{
  protected $ resume = 'I am junior developer i know nothink';
}
// create a class of vacancies for developers
class CompanyVacancies
{
  // "Job: Engineer PHP", and in order for the position to fall developers owning PHP,
  // specify for the expected type of the parameter of our empty interface Php
  public function engineerPhp (Php $ developer): void
  {
    . Echo 'PHP Engineer:' $ developer- & gt; getResume ();
  }
  // "Job: Engineer Javascript is", similarly, we expect developers owning Javascript is,
  // specify for the expected type of the parameter of our empty interface Javascript
  public function engineerJavascript (Javascript $ developer): void
  {
    . Echo 'Javascript Engineer:' $ developer- & gt; getResume ();
  }
  // "vacancy: novice developer" at this position does not require or Javascript is Php,
  // specify for the expected parameter type Developer.
  public function juniorDeveloper (Developer $ developer): void
  {
    . Echo 'Junior Developer:' $ developer- & gt; getResume ();
  }
}
// generate developed by each specialization
$ Backend = new BackendDeveloper ();
$ Frontend = new FrontendDeveloper ();
$ Fullstack = new FullstackDeveloper ();
$ Junior = new JuniorDeveloper ();
// generate jobs company
$ CompanyVacancies = new CompanyVacancies ();
// try to arrange the developers for each vacancy
$ CompanyVacancies- & gt; engineerPhp ($ backend); // display a summary of the developer backend
// $ companyVacancies- & gt; engineerPhp ($ frontend); // error, not fluent PHP frontend
$ CompanyVacancies- & gt; engineerPhp ($ fullstack); // display a summary of the developer fulstak
// $ companyVacancies- & gt; engineerPhp ($ junior); // error, not joon owns PHP
// $ companyVacancies- & gt; engineerJavascript ($ backend); // error, backend does not own Javascript
$ CompanyVacancies- & gt; engineerJavascript ($ frontend); // display a summary frontend developer
$ CompanyVacancies- & gt; engineerJavascript ($ fullstack); // display a summary of the developer fulstak
// $ companyVacancies- & gt; engineerJavascript ($ junior); // error, not joon owns Javascript
$ CompanyVacancies- & gt; juniorDeveloper ($ backend); // display a summary of the developer backend
$ CompanyVacancies- & gt; juniorDeveloper ($ frontend); // display a summary frontend developer
$ CompanyVacancies- & gt; juniorDeveloper ($ fullstack); // display a summary of the developer fulstak
$ CompanyVacancies- & gt; juniorDeveloper ($ junior); // display a summary of Juna

Answer 3

I do not think it’s right, because the interface is just to describe the behavior, and if anything in the interface and the interface is not needed. Imagine a keyboard without keys, to whom and why is it necessary?

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions