strategies for managing long class files in php

前端 未结 6 802
野的像风
野的像风 2021-02-09 08:37

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

6条回答
  •  青春惊慌失措
    2021-02-09 09:13

    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.

    EDIT

    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
      }
    }
    

提交回复
热议问题