Suppose we have two files:

  • main.php for further work he needs to get the text from the file
  • string.php allows you to get text from a file

As I see (receiving text in the abstract, the main logic):

1 option

<?php /* * main.php */ require_once 'string.php'; $text_1 = get_string_1(); $text_2 = get_string_2(); <?php /* * string.php */ function get_string_1() { $data = file_get_contents('text_first.txt' ); return $data; } function get_string_2() { $data = file_get_contents('text_second.txt' ); return $data; } 

Option 2

 <?php /* * main.php */ require_once 'string.php'; $str = new string(); $text_1 = str->get_1(); $text_2 = str->get_2(); <?php /* * string.php */ class string { function get_1 () { $data = file_get_contents('text_first.txt' ); return $data; } function get_2 () { $data = file_get_contents('text_second.txt' ); return $data; } } 

My question is to find out how to perform this task for example, and where can I read about it?

  • i.e., you have two (or more) text.files, and all you need is to display their contents on the page? - Edward
  • what's the point of comparing procedural and OOP approach everyone has the right to exist. In any case, in both of these options, the file name must be a parameter of a single function, it makes no sense to produce a bunch of the same code. If you need some kind of config, then set it as an associative array [ 'str1' => 'path_to_file1', ...] - teran
  • @Eduard "receiving text is abstract, the main logic" this task is absolutely abstract and has no relation to receiving text, imagine some other task, which option would you use and why? - Vladimir Alexandrov
  • @teran this task is abstract, of course, with the receipt of the text, you can do a lot more rationally - Vladimir Alexandrov
  • one
    as far as it is needed to solve the problem. You can select some functionality by marking all the corresponding functions in a file in a procedural version, and you can do the same in principle by wrapping it all in a class, which will also be in a separate file. Perhaps, if your task does not need to manipulate many objects one species that have some functionality, the OOP mb is not needed. After all, it turns out that in OOP, you apply a method to the data, that is, you call the object method. In the procedural version, you pass the data to the method as a parameter. - teran

0