在php中管理长类文件的策略 [英] strategies for managing long class files in php
问题描述
我有一堆想要移入类的函数.目前,它们被分为几个相当长的文件.我希望没有一个2500行文件,但是据我所知,您不能使用include将一个类拆分为多个文件.从理论上讲,我可以将函数分组到不同的类中,但是它们之间的联系非常紧密,以至于我觉得它们属于同一类,将它们拆分会减少一些我希望从程序方法中摆脱出来的实用性(具有共享属性,而不是几乎每个函数中都有一堆参数).
I've got a bunch of functions that I want to move into a class. They're currently split into a couple of fairly long files. I'd prefer not to have one 2500 line file, but as far as I can tell, you can't use include to split a class up into multiple files. In theory, I could group the functions in different classes, but they're closely related enough that I feel like they belong together, and splitting them will reduce some of the utility that I'm hoping to get from moving away from a procedural approach (with shared properties, rather than a bunch of parameters that are in nearly every function).
我知道这有点含糊,但是有什么建议/指针吗?如果重要的话,这是针对原型的,因此易于代码管理优先于安全性和性能.
I know this is a bit vague, but any suggestions/pointers? If it matters, this is for a prototype, so ease of code management takes precedence over security and performance.
更新:让我看看是否可以消除一些模糊性:
UPDATE: Let me see if I can remove some of the vagueness:
此类/函数集以复杂形式输出html.每个部分中有许多不同的部分和变体,具体取决于当前传递给函数的大约5或6个参数.我希望一次将参数定义为类的属性,然后从所有节创建方法中访问它们.如果使用子类,则这些属性的值将无法正确初始化,因此需要一个类. (嗯...除非我将它们定义为静态.我可能刚刚回答了我自己的问题.我必须看看是否有任何原因不起作用.)
This class/set of functions outputs the html for a complex form. There are many different sections and variations within each section, depending on about 5 or 6 parameters, which are currently passed into the functions. I was hoping to define the parameters once as properties of the class and then have access to them from within all of the section-creation methods. If I use sub-classes, the values of those properties won't be initialized properly, hence the desire for one class. (Hmm... unless I define them as static. I may have just answered my own question. I'll have to look to see if there's any reason that wouldn't work.)
我目前有一些混乱的功能,例如:
I've currently got a mess of functions like:
get_section_A ($type='foo', $mode='bar', $read_only=false, $values_array=array()) {
if ($this->type == 'foo') { }
else ($this->type == 'foo') { }
}
所以我最初想像的是:
class MyForm {
public $type; // or maybe they'd be private or
public $mode; // I'd use getters and setters
public $read_only; // let's not get distracted by that :)
public $values_array;
// etc.
function __constructor ($type='foo', $mode='bar', $read_only=false, $values_array=array()) {
$this->type = $type;
// etc.
}
function get_sections () {
$result = $this->get_section_A();
$result .= $this->get_section_B();
$result .= $this->get_section_C();
}
function get_section_A() {
if ($this->type == 'foo') { }
else { }
}
function get_section_B() {}
function get_section_C() {}
// etc. for 2500 lines
}
现在我在想类似的东西:
Now I'm thinking something like:
// container class file
class MyForm {
static $type
static $mode
static $read_only
static $values_array
// etc.
function __constructor ($type='foo', $mode='bar', $read_only=false, $values_array=array()) {
MyForm::$type = $type;
// etc.
}
function get_sections () {
$result = new SectionA();
$result .= new SectionB();
$result .= new SectionC();
}
}
// section A file
class SectionA extends MyForm {
function __constructor() {
if (MyForm::$type == 'foo') { }
else { }
}
function __toString() {
// return string representation of section
}
}
// etc.
或者可能我需要属性存在的FormSection的抽象类.
Or probably I need an abstract class of FormSection where the properties live.
还有其他想法/方法吗?
Any other ideas/approaches?
推荐答案
I'd split them up into as many classes as you want (or as many that make sense) and then define an autoloader to obviate inclusion headaches.
好吧,在看完更多代码后-我认为您正在错误地处理子类.您有很多针对$type
的if
语句,这向我表明 是多态性的基础.
Ok, after seeing more of your code - I think you're approaching subclasses wrong. You have lots of if
statements against $type
, which signals to me that that is what the polymorphism should be based on.
abstract class MyForm
{
protected
$mode
, $read_only
, $values
;
public function __construct( $mode, $read_only=false, array $values = array() )
{
$this->mode = $mode;
$this->read_only = (boolean)$read_only;
$this->values = $values;
}
abstract function get_section_A();
abstract function get_section_B();
abstract function get_section_C();
// final only if you don't want subclasses to override
final public function get_sections()
{
return $this->get_section_A()
. $this->get_section_B()
. $this->get_section_C()
;
}
}
class FooForm extends MyForm
{
public function get_section_A()
{
// whatever
}
public function get_section_B()
{
// whatever
}
public function get_section_C()
{
// whatever
}
}
这篇关于在php中管理长类文件的策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!