几年前写的东西了,翻出来重贴一下,呵呵
今天在群里面,有个叫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代码
- <?php
- function debug($var, $val)
- {
- echo "***DEBUGGING\nVARIABLE: $var\nVALUE:";
- if (is_array($val) || is_object($val) || is_resource($val)) {
- print_r($val);
- } else {
- echo "\n$val\n";
- }
- echo "***\n";
- }
-
- $c = mysql_connect();
- $host = $_SERVER["SERVER_NAME"];
-
- call_user_func_array('debug', array("host", $host));
- call_user_func_array('debug', array("c", $c));
- call_user_func_array('debug', array("_POST", $_POST));
- ?>
相信看了例子之后应该有点明白了吧?
我自己是这么理解这个函数的,如果说的不对,还望各位高手不要耻笑:
该函数真正的用法有点类似于函数重载,因为他的第一个参数是字符型的,也就是函数的名称,第二个参数是数组,我们可以当成该函数的各个参数,而事实上也就是这么用的,如果你看过我的前一篇文章:
PHP的伪重载 ,或许你能够理解,正是因为这个函数的存在,我发现函数重载也可以这样运用:
PHP代码
- <?php
-
-
-
-
- function otest1 ($a)
- {
- echo( '一个参数' );
- }
-
- function otest2 ( $a, $b)
- {
- echo( '二个参数' );
- }
-
- function otest3 ( $a ,$b,$c)
- {
- echo( '三个啦' );
- }
-
- function otest ()
- {
- $args = func_get_args();
- $num = func_num_args();
- call_user_func_array( 'otest'.$num, $args );
- }
-
- otest(1,2);
看到不?而我最初的写法,在
PHP的伪重载一文中有所提及,仅作参考。。。。
这些只是call_user_func_array的简易用法,在PHP4下测试过,而手册中还有一些将第一个参数当成数组来传入的例子,我在PHP4下是没有办法运行的,也许PHP5可以吧,但我不用PHP5的,也没有办法解释什么。谢谢各位
以前一直用PHP4的,现在用PHP5了,关于这个函数,大家可以看看thinkphp的functions.php中的getInstance方法,也是一个很好的诠释哦。
我只贴一种,其余的去看: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)
懒得写上什么,apache的rewrite一向是最让人头疼的。这是我几年前找的文章,继续转贴一下,毕竟以前的BLOG我也不知道能够被保留多久, 这个毕竟是自己的,只要没啥意外,会一直保留着的。
收藏自:竹笋炒肉
» 阅读全文
备份资料: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
更多看详细。。。
» 阅读全文
晚上,加班,开会,讨论,回家,打车,没钱,银行,取钱,失败,汗颜,凄凉,绝望,电话,转机,掏光,感慨