Dexter89 Posted April 22, 2017 Share Posted April 22, 2017 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! Quote Link to comment Share on other sites More sharing options...
Solution Jacques1 Posted April 22, 2017 Solution Share Posted April 22, 2017 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. Quote Link to comment Share on other sites More sharing options...
Dexter89 Posted April 23, 2017 Author Share Posted April 23, 2017 (edited) 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 April 23, 2017 by Dexter89 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted April 23, 2017 Share Posted April 23, 2017 [...] 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.