📜 ⬆️ ⬇️

Each class has its own file.

There is a class that contains a couple of dozen functions that can be grouped into 3-4 groups by purpose. It would be convenient for me if these groups of functions were put into separate files, but at the same time I didn’t lose the structure of the class so that the class would be left out as a class. I'm tired of scrolling long files with classes.

Those. I want the same class to be represented by several files:
power_class.php
power_class_g1.php
power_class_g2.php
power_class_g3.php

How to implement it competently?

1. I have one class on which the site is built. In this class there are common functions, and there are individual for each section. I do not want to write classes for each section. It would be more convenient for me to take out functions from files of this class. Fetching classes too crap.
')
2. I have Zend Studio, I do not like this concept of folding classes, deployment. All these IDE editors are morally obsolete. In large projects, you have to jump in folders and files, like an athlete, to open dozens of files and other crap.

I would generally do this: each function is a file. And then so that it would be collected somehow automatically in one or several files and poured onto a server ... Or, for example, it would be possible to store functions in a relational database ... but this is another song.

I would like editors to virtually assemble the functions they need now in a single editing window. For example, one function from the x.php file, another from the z.php file, one function from the class of some other from the other ...

3. It seems to me that the thinking of the programmer is held back by the file system and the concept of the text editors. We think at the level of folders and files, while databases exist for a long time.

For example, to choose functions with the name Convert in a large project - it is necessary to wait when the search will go around all the folders and files, and if it is on a remote server - then write everything is gone. And then if you try to edit them - it means opening ten files with ten windows ...

DECISION

I see this solution to my problem.

1. A function is called that assembles a class from several files into a single file class_power.php
acts simply as a template engine
function compile_class(){
}

2. Then the generated class_power.php is included.
3. The structure of the class that is going to class_power.php
class_main.php - file, which describes the structure of the class
Class powerclass
{
var $var1;
#class_part1.php#
#class_part2.php#
#class_part3.php#
}

class_part1.php - part of the class as a set of functions
class_part2.php - part of the class as a set of functions
class_part3.php - part of the class as a set of functions

This way you can collect several files with the same functions, which I see may also be useful.

Another solution:

function load_mclass($class="power_class"){
$res[]="class $class{\n";
foreach(glob($class."*") as $c)
$res[]= "<?php", "class $class{", "}", "?>" file_get_contents($c);
$res[]="\n}";
$res $class.m.php
$class.m.php
}


This is necessary in order not to waste your time on scrolling through a long file, and also to group functions related by meaning or purpose, and at the same time not to lose the class structure.

Source: https://habr.com/ru/post/64749/


All Articles