I decided to make a TV program cleaner. The cleaner will have two modes: the first is the loading and processing of text files (there may be up to 30, but the names do not change), the second is when you copy the text of one file, insert it into <textarea> and get the result. For the first mode, almost everything is ready and it seems to be working for now. I made a class and registered variables:

 public $fileName, $startTime, $endTime, $findAndDelete; public function Raw(){...} public function Del($array){...} 

Here are indicated, respectively:

the name of the file to be processed, the start time of the TV program, the time of its end (the rest of the time is deleted or transferred to the next day). Raw() reads files line by line using file() to return to normal appearance and return an array. Del() removes all unnecessary in accordance with $findAndDelete from the resulting array Raw() and returns the processed array.

For example, for a discovery channel would look like this:

 $disc_eu_s = new Channel; $disc_eu_s->fileName = 'disc_eu_s.txt'; $disc_eu_s->startTime = '10:00'; $disc_eu_s->endTime = '02:00'; $disc_eu_s->findAndDelete = array( '~с Беаром Гриллсом~u', '~с Эдом Стаффордом~u' ); result( $disc_eu_s->del( $disc_eu_s->raw() ) ); 

So the question is.

How to correctly “tie” the class to the second mode so that both modes process the text equally regardless of whether I uploaded it as a file or opened the file and copied and pasted the text?

In the first mode, files are read line by line and in the second mode, the inserted text will also be read line by line explode("\n", ...) . The only thing I did in vain was to set up the public $fileName , which is useless for the second mode. By the way, here’s something else in common for both modes (which can help in my question) - this is the first line in the file and the first line in the inserted text - the name of the channel is indicated here, but how to tie it up correctly?

Ps I forgot to mark one important point in my question. Processing in the first mode occurs according to the plan, that is, if the file is disc_eu_s.txt, then the start and end times, as well as findAndDelete, are appropriate for it. And if I open the same file, copy the content and paste it into the textArea, then the application (if it can be said so) must recognize that it is text as from disc_eu_s.txt and is edited according to its variables. Therefore, I wanted to become attached to the first line with the name of the channel ...

    1 answer 1

    I think in this case, your class violates the principle of SOLID SRP. It turns out that the class now has two duties: data acquisition and their processing in the del method.

    I would do this:

    1) Encapsulated data in the constructor.

    2) In the Channel constructor passed an additional object instead of disc_eu_s.txt

    It would turn out like this:

    $disc_eu_s = new Channel(new DataFile('$fileName'),'10:00', '02:00', findAndDelete); //для режима с файлом

    $disc_eu_s = new Channel(new DataTextArea(),'10:00', '02:00', findAndDelete);//для режима с полем. Можно передавать конструктору название поля, тут уж как вы считаете нужным.

    $disc_eu_s->fileName from class remove.

    Pass a data object to the Raw() method that will call its methods to retrieve strings.

     interface idata{ public function Raw(); } Class DataFile implements idata{ ... } Class DataTextArea implements idata{ ... } 

    And in DataTextArea already implement all the functionality for processing data from the form.

    • thank! I only study php, I did not know about the interface before your message, but I already read it and now I know something (though I haven’t found in which file to store the interface , where is the class or where is it?), I will definitely use your advice! How to deal with del() ? Where to put it in the interface ? - forever_young
    • one
      It is better to put in a separate file in the same directory as the class files. While the project is small, it may seem redundant, but when it grows, the newly created classes may need to be inherited from it. - four
    • I forgot to mark one important point in my question. Processing in the first mode occurs according to the plan, that is, if the file is disc_eu_s.txt, then the start and end times, as well as findAndDelete are appropriate. And if I open this file, copy the content and paste it into the textArea, then the application (if it can be said so) must recognize that it is the text disc_eu_s.txt and is edited according to its variables. Therefore, I wanted to become attached to the first line with the name of the channel ... - forever_young
    • del() left where it is now. The duty of the class will work with strings derived from those classes and delete what you do not need. idata - in this case, the so-called. marker interface. Interfaces cannot contain implementations. ___And generally general advice. OOP is a methodology, paradigm and php is not the best language to start understanding it. - four
    • Therefore, I wanted to become attached to the first line with the name of the channel ... - Explain this. In general, your classes should not know about the features of their use. All that concerns variables and peculiarities of use should be decided "above." - four