手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表全部文章

两年前写的php之call_user_func_array的简易用法

几年前写的东西了,翻出来重贴一下,呵呵


今天在群里面,有个叫lewis的在问call_user_func_array的用法,因为之前一直没有用过,也不能说什么,于是看一下手册,发现是这么写的:

call_user_func_array

(PHP 4 >= 4.0.4, PHP 5)

call_user_func_array --  Call a user function given with an array of parameters

Description

mixed call_user_func_array ( callback function, array param_arr )

Call a user defined function given by function, with the parameters in param_arr.
然后还有一个例子:

PHP代码
  1. <?php  
  2. function debug($var$val)   
  3. {  
  4.     echo "***DEBUGGING\nVARIABLE: $var\nVALUE:";  
  5.     if (is_array($val) || is_object($val) || is_resource($val)) {  
  6.         print_r($val);  
  7.     } else {  
  8.         echo "\n$val\n";  
  9.     }  
  10.     echo "***\n";  
  11. }  
  12.   
  13. $c = mysql_connect();  
  14. $host = $_SERVER["SERVER_NAME"];  
  15.   
  16. call_user_func_array('debug'array("host"$host));  
  17. call_user_func_array('debug'array("c"$c));  
  18. call_user_func_array('debug'array("_POST"$_POST));  
  19. ?>   
相信看了例子之后应该有点明白了吧?
我自己是这么理解这个函数的,如果说的不对,还望各位高手不要耻笑:
     该函数真正的用法有点类似于函数重载,因为他的第一个参数是字符型的,也就是函数的名称,第二个参数是数组,我们可以当成该函数的各个参数,而事实上也就是这么用的,如果你看过我的前一篇文章:PHP的伪重载 ,或许你能够理解,正是因为这个函数的存在,我发现函数重载也可以这样运用:

PHP代码
  1. <?php  
  2. /** 
  3. * 例子写完后,本来认为完事了,结果遇到有人问call_user_func_array(),看了一下手册 
  4. * 原来,我上面的那个test函数还可以精简成如下的例子, 
  5. */  
  6. function otest1 ($a)  
  7. {  
  8.     echo'一个参数' );  
  9. }  
  10.   
  11. function otest2 ( $a$b)  
  12. {  
  13.     echo'二个参数' );  
  14. }  
  15.   
  16. function otest3 ( $a ,$b,$c)  
  17. {  
  18.     echo'三个啦' );  
  19. }  
  20.   
  21. function otest ()  
  22. {  
  23.     $args = func_get_args();  
  24.     $num = func_num_args();  
  25.     call_user_func_array( 'otest'.$num$args  );  
  26. }  
  27.   
  28. otest(1,2);  
看到不?而我最初的写法,在PHP的伪重载一文中有所提及,仅作参考。。。。

这些只是call_user_func_array的简易用法,在PHP4下测试过,而手册中还有一些将第一个参数当成数组来传入的例子,我在PHP4下是没有办法运行的,也许PHP5可以吧,但我不用PHP5的,也没有办法解释什么。谢谢各位


以前一直用PHP4的,现在用PHP5了,关于这个函数,大家可以看看thinkphp的functions.php中的getInstance方法,也是一个很好的诠释哦。

 

Tags: 自定义, php, 伪重载, 多态

MYSQL官方的文章:几种无限分类的算法……

我只贴一种,其余的去看:http://dev.mysql.com/tech-resources/articles/hierarchical-data.html

The Adjacency List Model

Typically the example categories shown above will be stored in a table like the following (I'm including full CREATE and INSERT statements so you can follow along):

CREATE TABLE category(
category_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) NOT NULL,
parent INT DEFAULT NULL);


INSERT INTO category
VALUES(1,'ELECTRONICS',NULL),(2,'TELEVISIONS',1),(3,'TUBE',2),
(4,'LCD',2),(5,'PLASMA',2),(6,'PORTABLE ELECTRONICS',1),
(7,'MP3 PLAYERS',6),(8,'FLASH',7),
(9,'CD PLAYERS',6),(10,'2 WAY RADIOS',6);

SELECT * FROM category ORDER BY category_id;

+-------------+----------------------+--------+
| category_id | name | parent |
+-------------+----------------------+--------+
| 1 | ELECTRONICS | NULL |
| 2 | TELEVISIONS | 1 |
| 3 | TUBE | 2 |
| 4 | LCD | 2 |
| 5 | PLASMA | 2 |
| 6 | PORTABLE ELECTRONICS | 1 |
| 7 | MP3 PLAYERS | 6 |
| 8 | FLASH | 7 |
| 9 | CD PLAYERS | 6 |
| 10 | 2 WAY RADIOS | 6 |
+-------------+----------------------+--------+
10 rows in set (0.00 sec)

In the adjacency list model, each item in the table contains a pointer to its parent. The topmost element, in this case electronics, has a NULL value for its parent. The adjacency list model has the advantage of being quite simple, it is easy to see that FLASH is a child of mp3 players, which is a child of portable electronics, which is a child of electronics. While the adjacency list model can be dealt with fairly easily in client-side code, working with the model can be more problematic in pure SQL.

Retrieving a Full Tree

The first common task when dealing with hierarchical data is the display of the entire tree, usually with some form of indentation. The most common way of doing this is in pure SQL is through the use of a self-join:

SELECT t1.name AS lev1, t2.name as lev2, t3.name as lev3, t4.name as lev4
FROM category AS t1
LEFT JOIN category AS t2 ON t2.parent = t1.category_id
LEFT JOIN category AS t3 ON t3.parent = t2.category_id
LEFT JOIN category AS t4 ON t4.parent = t3.category_id
WHERE t1.name = 'ELECTRONICS';

+-------------+----------------------+--------------+-------+
| lev1 | lev2 | lev3 | lev4 |
+-------------+----------------------+--------------+-------+
| ELECTRONICS | TELEVISIONS | TUBE | NULL |
| ELECTRONICS | TELEVISIONS | LCD | NULL |
| ELECTRONICS | TELEVISIONS | PLASMA | NULL |
| ELECTRONICS | PORTABLE ELECTRONICS | MP3 PLAYERS | FLASH |
| ELECTRONICS | PORTABLE ELECTRONICS | CD PLAYERS | NULL |
| ELECTRONICS | PORTABLE ELECTRONICS | 2 WAY RADIOS | NULL |
+-------------+----------------------+--------------+-------+
6 rows in set (0.00 sec)

Tags: mysql, 无限分类, 算法, 存储结构, 官方

apache rewrite 详解

懒得写上什么,apache的rewrite一向是最让人头疼的。这是我几年前找的文章,继续转贴一下,毕竟以前的BLOG我也不知道能够被保留多久, 这个毕竟是自己的,只要没啥意外,会一直保留着的。

收藏自:竹笋炒肉

» 阅读全文

Tags: 转贴, apache, rewrite, 解释

SQL语句导入导出大全(转)

备份资料:Copy from ---> http://php.mydict.com/ziliao/7/2006_05/SQLYuJuDaoRuDaoChuDaQuan3016_1.html



/******* 导出到excel
EXEC master..xp_cmdshell 'bcp SettleDB.dbo.shanghu out c:\temp1.xls -c -q -S"GNETDATA/GNETDATA" -U"sa" -P""'

/*********** 导入Excel
SELECT *
FROM OpenDataSource( 'Microsoft.Jet.OLEDB.4.0',
'Data Source="c:\test.xls";User ID=Admin;Password=;Extended properties=Excel 5.0')...xactions

更多看详细。。。

» 阅读全文

Tags: sql, 导入, 导出, 详解, 数据库

感慨

晚上,加班,开会,讨论,回家,打车,没钱,银行,取钱,失败,汗颜,凄凉,绝望,电话,转机,掏光,感慨

Tags: 感慨, 汗颜, 凄凉