concept: php multidimentional methods


/ Published in: PHP
Save to your folder(s)

A class i started to implement, which ended up in a working concept
However I'm not sure if it's a legal way of programming since I unnessesery
changes the structure of php without some real value except nicer structure

For now it remain a concept

Usage:

concept::create()->create1();
concept::create()->create2();
concept::test()->test1();
concept::test()->test2();


Copy this code and paste it in your HTML
  1. /*
  2.  * A class i started to implement, which ended up in a working concept
  3.  * However I'm not sure if it's a legal way of programming since I unnessesery
  4.  * changes the structure of php without some real value except nicer structure
  5.  *
  6.  * For now it remain a concept
  7.  *
  8.  * Usage:
  9.  *
  10.  * concept::create()->create1();
  11.  * concept::create()->create2();
  12.  * concept::test()->test1();
  13.  * concept::test()->test2();
  14.  *
  15.  */
  16.  
  17. class concept
  18. {
  19. private static $functions = array(
  20. 'create' => '__callStatic',
  21. 'create1' => 'create',
  22. 'create2' => 'create',
  23. 'test' => '__callStatic',
  24. 'test1' => 'test',
  25. 'test2' => 'test');
  26. private $create = false;
  27.  
  28. private function create1()
  29. {
  30. \pre::dump('create1');
  31. }
  32.  
  33. private function create2()
  34. {
  35. \pre::dump('create2');
  36. }
  37.  
  38. private function test1()
  39. {
  40. \pre::dump('test1');
  41. }
  42.  
  43. private function test2()
  44. {
  45. \pre::dump('test2');
  46. }
  47.  
  48. public static function __callStatic($name, $argument)
  49. {
  50. if(isset(self::$functions[$name]) && self::$functions[$name] == '__callStatic')
  51. {
  52. $className = __class__;
  53. $new = new $className;
  54. $new->$name = true;
  55.  
  56. return $new;
  57. }
  58. else
  59. {
  60. throw new \Exception('Static function in class <b>sql</b> does not exists; ' . $name);
  61. }
  62. }
  63.  
  64. public function __call($name, $argument)
  65. {
  66. $function = isset(self::$functions[$name]) ? self::$functions[$name] : null;
  67.  
  68. $call = array($this, $name);
  69.  
  70. if(isset($this->$function) && $this->$function === true && is_callable($call))
  71. {
  72. call_user_func_array($call, $argument);
  73. }
  74. else
  75. {
  76. throw new \Exception('Function in class <b>sql</b> does not exists; ' . $name);
  77. }
  78. }
  79. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.