Jump to content

Split one class in multiple files.


Dexter89
Go to solution Solved by Jacques1,

Recommended Posts

Hi everybody. I'm new here. Please help me to find a solution to my problem.

 

So...

 

I have a file with a class named Admin. Here is an example:

 

Admin.php

class Admin
{
    public static function Set_something_1()  { }
    public static function Set_something_2()  { }
    public static function Set_something_3()  { }

    public static function Get_something_1()  { }
    public static function Get_something_2()  { }
    public static function Get_something_3()  { }

    /* And so on */
}

Now, my problem is that the class has to many methods and, anyway, look at each name of methods...

 

So what I have in mind:

 

Admin.php

class Admin
{
    // include Admin_Set class
    // include Admin_Get class
}

Admin_Set.php

class Admin_Set
{
    public static function Set_something_1()  { }
    public static function Set_something_2()  { }
    public static function Set_something_3()  { }

    /* And so on */
}

Admin_Get.php

class Admin_Get
{
    public static function Get_something_1()  { }
    public static function Get_something_2()  { }
    public static function Get_something_3()  { }

    /* And so on */
}

My problem ?

 

I still want to call my methods using Admin::Set_something_1() and Admin::Get_something_1()

 

So what's the best way to do that ?

 

Again. My problem is that the Admin.php was to big to store up to 20 methods. So I want to "split" the file. As you probably already saw, there are two "types" of methods Set_ and Get_. So, it's even properly to do that.

 

I'm listening.

 

Thanks!

Link to comment
Share on other sites

  • Solution

What makes you think the class is “too big” for a single file? I can see how a class may become too complex, but “too big” sounds like you use really bad tools for programming.

 

In any case: If you have plenty of hand-coded getters and setters, consider a more intelligent approach like virtual methods. Then there are traits which allow you to outsource methods. But don't abuse this to fix tool-related problems.

Link to comment
Share on other sites

I wanna say that the file (Admin.php) was to hard to read. Or, of course, the class is to complex.... for just one file...

 

Anyway, even if your answer sounds "bad" (as from master to beginner), seems like Traits is exactly what I was looking for.

 

Thank you very much!

Edited by Dexter89
Link to comment
Share on other sites

[...] seems like Traits is exactly what I was looking for.

 

I very much doubt that, and I already regret giving you that option.

 

This is what you need to do:

  • Get a proper IDE which allows you to collapse method definitions. Then you can hide all the code you don't currently need and not worry about “big files”.
  • Throw away repetitive code. There's absolutely no reason to clutter your class with a long list of getters and setters. The __call() method allows you to catch calls of undefined methods and then run arbitrary code. So you can have “virtual” getters and setters:

    <?php
    
    class GetterSetterTest
    {
        const GETTABLE_ATTRIBUTES = ['foo', 'bar'];
    
        const SETTABLE_ATTRIBUTES = ['foo'];
    
        private $foo;
    
        private $bar;
    
        public function __call($name, $arguments)
        {
            $isGetter = (strpos($name, 'get') === 0);
            $isSetter = (strpos($name, 'set') === 0);
            if (($isGetter || $isSetter) && strlen($name) > 3)
            {
                $attribute = strtolower($name[3]).substr($name, 4);
                if ($isGetter)
                {
                    if (!in_array($attribute, static::GETTABLE_ATTRIBUTES))
                    {
                        throw new RuntimeException('No getter for attribute '.$attribute.'.');
                    }
    
                    if (count($arguments) != 0)
                    {
                        throw new InvalidArgumentException('Expected zero arguments for getter.');
                    }
    
                    return $this->$attribute;
                }
                elseif ($isSetter)
                {
                    if (!in_array($attribute, static::SETTABLE_ATTRIBUTES))
                    {
                        throw new RuntimeException('No setter for attribute '.$attribute.'.');
                    }
    
                    if (count($arguments) != 1)
                    {
                        throw new InvalidArgumentException('Expected exactly one argument for setter.');
                    }
    
                    $this->$attribute = $arguments[0];
    
                    return;
                }
            }
    
            throw new RuntimeException('Call to undefined method '.$name.'.');
        }
    }
    
    
    
    $getterSetterTest = new GetterSetterTest();
    
    // call "virtual" setter
    $getterSetterTest->setFoo(3);
    
    // call "virtual" getter
    var_dump($getterSetterTest->getFoo());
    
    // try invalid setter
    $getterSetterTest->setBar(3);
    
  •  

    If you still have “too much” code, post it here.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.