strategies for managing long class files in php

前端 未结 6 801
野的像风
野的像风 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 08:51

    Usually I do something like this:

    class one
    {
        public function __get($key)
        {
            // require __DIR__ / $key . php
            // instanciate the sub class
        }
    
        public function mainMethod()
        {
        }
    }
    
    class one_subOne extends one
    {
        public function otherMethod()
        {
        }
    }
    
    class one_subTwo extends one
    {
        public function anotherMethod()
        {
        }
    }
    
    $one->mainMethod();
    $one->subOne->otherMethod();
    $one->subTwo->anotherMethod();
    

提交回复
热议问题