概念:php多维方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了概念:php多维方法相关的知识,希望对你有一定的参考价值。

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();
  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. }

以上是关于概念:php多维方法的主要内容,如果未能解决你的问题,请参考以下文章

Php多维数组与混合for和Foreach循环

如何比较两个PHP多维数组

PHP填充多维关联数组 - 最简单的方法

PHP最好的MD5多维数组方法?

PHP提取多维数组指定一列的方法大全

PHP提取多维数组指定一列的方法大全