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

两年前写的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, 伪重载, 多态

服务器正式启用ImagIck组件

Imagick的DLL在windows上本来无法装上,前面一篇博客里介绍说团队好友hihiyou帮忙找了一个DLL,可以用在PHP 5.2.X上面的,今天一大早COPY到服务器上,并扩展出来。。

看图说话,OH YEAH。可惜。。。sablog不支持Imagick,它还是用GD的,不知道新版本会不会采用。

好象小图看不太清楚。还是点击一下看大图吧。。。

大小: 31.27 K
尺寸: 398 x 376
浏览: 2614 次
点击打开新窗口浏览全图

Tags: imagick, 安装

关于Imagick的使用

ImagIck这个控件应该算是最近两年来,各个WEB程序员都比较推荐的一个编辑图片的软件。
其实这也是一个分支吧?如果我没有记错,都是从XXX(忘了是哪个了)分出来的,一个是MagickWand,另一个就是这个Imagick了,只是MagicWand好象比较难以控制,并不如这个Imagick方便,再加上Imagick的类及使用方法也集成在PHP手册里了。

本来我是安装了ImageMagick-6.2.5-4-Q16-windows-dll.exe的,然而,愿望总是美好的,结果在Apache下运行的时候,总是提示挂接不上某个动态链接库,装回5.5.7后一切正常,但却无法使用PHP手册里所提供的函数。

本来都想放弃了,可是同事在单位里的Linux上装上了这个软件,但由于我们的PHP版本太低,在大多数的情况下都是直接使用命令行进行操作的。

为了适应将来的PHP版本升级,所以不得不学习这个软件,在我以前的贴子里,也贴过了Imagick官网和一些例子的实现网站。
今后我也会重新拾起代码,尽量、争取把官网的例子也逐步一一实现,或者大部分常用的实现一下。

感谢团队好友hihiyou(http://www.hihiyou.com)为我找了一个可以在windows下使用的Imagick的DLL,可以让我开始学习它。

先来个例子,黑黑。。。

PHP代码
  1. <?php  
  2. header('Content-type: image/gif');  
  3.   
  4. $image = new Imagick('google_logo.gif');  
  5. $image->adaptiveResizeImage(400,NULL);  
  6.   
  7. echo $image;  
  8. ?>  

原图:

 

大小: 8.36 K
尺寸: 276 x 110
浏览: 2353 次
点击打开新窗口浏览全图

重生成:

大小: 18.96 K
尺寸: 400 x 159
浏览: 2328 次
点击打开新窗口浏览全图

Tags: imagick, php, extension, dll

闲着无聊,用WPS生成文档

不好意思,该操作只能在windows服务器上才能执行,是因为,只有windows服务器才支持COM功能,而且,必须安装wps(这点应该不难,因为WPS是免费软件)
免费软件这点太重要了,如果你要生成WORD,你不可能安装一个OFFICE吧,office太贵了。

PHP代码
  1. <?php  
  2. $wps = new COM("WPS.Application");  
  3. $wps->Visible = false;  
  4. $doc = $wps->Documents->Add();  
  5. $doc->Range->Text = 'WPS FOR PHP test';  
  6. $doc->Paragraphs[1]->Alignment = 1;  
  7. $doc->Shapes->AddPicture("http://img.kingsoft.com/publish/kingsoft/images/gb/sy/logo.gif", 100, 50, 148, 60);  
  8. $doc->SaveAs('c:\\temp\\test1.wps');  
  9. $doc->Close();  
  10. $wps->Quit();  
  11. unset( $doc , $wps );  
  12. ?>  

代码很简单,只是添加了一个文字和一个图片而己。但是,证明了是可以使用这个功能。
大家别忘了,WPS的附加功能可是很多的,比如把文件转为PDF等格式。也就是说, 如果是在windows服务器上,我们要生成PDF,可以不使用那些什么fpdf类库,而是使用WPS先生成一个WORD格式的文档,然后直接exportPdf就可以了。要知道OFFICE默认并不支持这种功能。黑黑

这也是一种思路嘛,思路决定出路,转换一种思路,可能就是海阔天空。

这里是WPS的开发者网站:http://wps.kingsoft.com/apply/engineer.shtml,从这里可以下载API:http://wps.kingsoft.com/download/WPSAPI.zip

Tags: php, wps, com, wps application

为windows的WEB服务器添加高性能的FTP组件

在windows服务器里面,如果我们要用FTP函数,速度应该是很慢的,而且效率不高,如果有大量文件需要上传,用自带的FTP函数,恐怕是要死人的吧。
windows服务器,windows哦,可以装N多软件的windows哦。虽然不建议在服务器上装上很多软件,但是也可以装一些FTP软件的嘛。这里以cuteftp举例。
cuteftp安装完毕后,会在软件目录里有一个Scripts目录,现在的版本不象很久以前的了,如今的版里只有一个Sample.vbs,记得几年前的cuteftp里面会有各种各样的vbs文件的。
闲话不多说,打开vbs文件,内容为:

ASP/Visual Basic代码
  1. 'This default template script is in VBScript. You can write scripts in your language of choice and save them with the proper extension, or use your an editor specific to that language.  
  2. 'See the TESDK help file for more details on how the scripting feature works and for information on each supported method and property.  
  3. 'You must have Windows Scripting Host installed for the COM enabled engine to work. Run the Transfer Engine once to register it (as a COM object) on the target system.  
  4. 'If you're having problems with running scripts while not logged in, or when trying to run them using MS scheduler, refer to our online knowledgebase for help (http://www.globalscape.com/support)  
  5. 'Look into c:\temp folder to observe local activity (for testing purposes) or right click on the Transfer Engine icon in the systray and select "show current transfers"  
  6. 'This sample script performs an anonymous login to ftp://ftp.globalscape.net  
  7.    'First declare a variable called Mysite. This will hold the reference to the TE COM object.  
  8.    Dim MySite  
  9.    'Create a connection object and assign it to the variable  
  10.    Set MySite = CreateObject("CuteFTPPro.TEConnection")  
  11.    ' Now set each property for the site connection   
  12.    ' You can omit this section to use the default values, but you should at least specify the Host  
  13.    'The default Protocol is FTP, however SFTP (SSH2), FTPS (SSL), HTTP, and HTTPS can also be used)  
  14.    MySite.Protocol = "FTP"  
  15.    MySite.Host = "ftp.globalscape.com"  
  16.    'following lines are optional since the default is anonymous if no login and password are defined  
  17.    MySite.Login = "anonymous"  
  18.    MySite.Password = "user@user.com"  
  19.    'if necessary, use the UseProxy method and ProxyInfo or SocksInfo properties to connect through a proxy server  
  20.    MySite.UseProxy = "BOTH"  
  21.    'now connect to the site (also called called implicitly when most remote methods are called)  
  22.    MySite.Connect  
  23.    'perform some logic to verify that the connection was made successfully  
  24.    If (Not Cbool(MySite.IsConnected)) Then    
  25.       MsgBox "Could not connect to: " & MySite.Host & "!"  
  26.       Quit(1)  
  27.    End If  
  28.    'The script will now check to see if the local folder c:\temp exists and will create it if necessary  
  29.    If (Not (MySite.LocalExists("c:\temp"))) Then  
  30.       MySite.CreateLocalFolder "c:\temp"  
  31.    End If  
  32.    'Change TE's local working folder to to c:\temp  
  33.    MySite.LocalFolder = "c:\temp"  
  34.    'Check for existence of remote folder "/pub/cuteftp"  
  35.    b = MySite.RemoteExists("/pub/cuteftp/")  
  36.    If (Not CBool(b)) Then  
  37.       'Verify existence of remote folder  
  38.       MsgBox "Remote folder not found!. Please make sure that the Pub folder exists on the remote site"  
  39.       Quit(1)  
  40.    End If  
  41.    'Now download the index file to the local destination folder  
  42.    MySite.Download "/pub/cuteftp/index.txt"  
  43.    'Complete.  Show the status of this transfer.  
  44.    MsgBox "Task done, final status is '" + MySite.Status + "'"  
  45.   MySite.Disconnect  
  46.   MySite.Close  
  47. 'End of sample script. You can save you script and then run it by either selecting it from the Tools > Run Script menu or by double clicking on the script file in Windows  

看到这样的文件,你应该知道如何调用一些FTP软件自带的方法了吧?现在,我们用PHP模拟一遍。。。。。
请看详细内容

» 阅读全文

Tags: ftp, com, windows, cuteftp, vbs