PHP開(kāi)發(fā)學(xué)堂:PHP-GTK介紹及其應(yīng)用
Jun 01, 2016 pm 02:25 PM1. PHP-GTK介紹
1.1 PHP-GTK
PHP-GTK是PHP的延伸模組,它可以讓程序設(shè)計(jì)師寫(xiě)出在客戶端執(zhí)行的、且獨(dú)立的GUI的程序。這個(gè)模組不允許在瀏覽器上顯視GTK+的程序,它一開(kāi)始就是開(kāi)發(fā)來(lái)寫(xiě)?yīng)毩⒌腉UI程序的。
1.2 GTK
GTK原本是為GIMP,一個(gè)GUI的影像處理軟體而開(kāi)發(fā)的。GTK+是GIMP的套裝工具。GTK+從這里開(kāi)始發(fā)展,直到現(xiàn)在已經(jīng)成為Gnome的中心(Gnome是一個(gè)桌面環(huán)境)。后來(lái)GTK+也已經(jīng)被推廣到BeOS和Win32,使得它成為PHP延伸模組的最佳選擇,維持PHP可以跨平臺(tái)并可以用PHP為L(zhǎng)inux,BeOS,Windows等平臺(tái)開(kāi)發(fā)視窗接口的程序。
2. PHP-GTK概念
2.1 前言
接下來(lái)就要教各位一點(diǎn)點(diǎn)比較觀念性的東西羅┅因?yàn)檫@章的概念都是非常重要的,所以就算不懂,也還是要慢慢的看懂它,不然┅以后就┅。還有,接下來(lái)的內(nèi)容不建議沒(méi)有程序設(shè)計(jì)經(jīng)驗(yàn)的讀者閱讀,因?yàn)橛泻芏嗟挠^念很容易會(huì)搞不清楚。還有,接下來(lái)該用英文的部分我都會(huì)用英文,這樣大家在看國(guó)外文件的時(shí)候才不會(huì)不知所措,加油吧!!如果對(duì)本章有任何不懂之處,請(qǐng)自行查閱
PHP-GTK Manual:http://gtk.php.net/manual/en/
2.2 Widget(s)
Widget是一個(gè)GUI程序中基本的functions和forms。最常用的幾個(gè)Widget是:label、button、window、frame和text box。所有的widget都是來(lái)自于一個(gè)抽象的基本class─GtkWidget。每個(gè)widget都是一個(gè)class
一個(gè)Widget一生大概都有五個(gè)時(shí)期:
1. 建立(Creation):宣告一個(gè)對(duì)象(declaring an object)
2. 放置(Placement):將它加入一個(gè)容器中(adding it to a container)
3. 信號(hào)連接(Signal Connection):接收信號(hào)以及進(jìn)行動(dòng)作(the action it will perform)
4. 顯示(Display):它是否是可見(jiàn)的(whether it is viewable or not)
5. 刪除(Destruction):關(guān)閉程序(closing of a program)
2.3 Container(s)
Container是一個(gè)可以包含其它widget的widget。大部分的widget都是container,例如:GtkWindow、GtkTable和GtkBox。除了這點(diǎn)之外,container跟其它的widget沒(méi)兩樣,也可以被放到其它c(diǎn)ontainer去。而所有的container都是來(lái)自于一個(gè)class─GtkContainer,本身來(lái)自于GtkWidget的class。所以container也是widget的一種。
2.4 Signal(s)
當(dāng)程序設(shè)計(jì)師在程序中做了一個(gè)動(dòng)作時(shí),程序需要有一個(gè)動(dòng)作來(lái)回應(yīng)使用者的動(dòng)作。Signals使程序可以知道使用者做了動(dòng)作并可以觸發(fā)適合的回應(yīng)。
例如,當(dāng)使用者按了一個(gè)可以開(kāi)新視窗的按鈕(GtkButton),程序認(rèn)出這個(gè)請(qǐng)求,于是就開(kāi)了一個(gè)新的視窗。這件事可以經(jīng)由signal來(lái)做到。當(dāng)按鈕按下去之后,會(huì)使widget發(fā)出一個(gè)signal,接著再由該signal觸發(fā)callbacks,產(chǎn)生一個(gè)新的視窗(GtkWindow)。
2.5 Callback(s)
Callback就是當(dāng)signal送出之后,被signal喚起的function。Callback會(huì)執(zhí)行function傳回一個(gè)值或是做一個(gè)動(dòng)作。Callback就是signal的handler funciton。它可以是該signal的預(yù)設(shè)handler或著是程序設(shè)計(jì)師定義的function。要建立一個(gè)callback,就必須把function connect 到 signal。
2.6 Signal Inheritance(繼承)
和methods一樣,signals可以被對(duì)象繼承。一個(gè)widget可以送出任何它的parent widget可以送出的還有它自己特有的signal。
2.7 Connecting Signals
你必須為PHP-GTK指定一個(gè)callback function當(dāng)signal送出時(shí)來(lái)對(duì)signal做回應(yīng)。把一個(gè)signal連接到一個(gè)function可以用connect() 這個(gè)object 方法達(dá)成。
如下:
//建立一個(gè)GtkWindow
$window = &new GtkWindow();
//將"destroy" signal用connect() 方法連接到shutdown函式
$window->connect("destroy", "shutdown");

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Common problems and solutions for PHP variable scope include: 1. The global variable cannot be accessed within the function, and it needs to be passed in using the global keyword or parameter; 2. The static variable is declared with static, and it is only initialized once and the value is maintained between multiple calls; 3. Hyperglobal variables such as $_GET and $_POST can be used directly in any scope, but you need to pay attention to safe filtering; 4. Anonymous functions need to introduce parent scope variables through the use keyword, and when modifying external variables, you need to pass a reference. Mastering these rules can help avoid errors and improve code stability.

To safely handle PHP file uploads, you need to verify the source and type, control the file name and path, set server restrictions, and process media files twice. 1. Verify the upload source to prevent CSRF through token and detect the real MIME type through finfo_file using whitelist control; 2. Rename the file to a random string and determine the extension to store it in a non-Web directory according to the detection type; 3. PHP configuration limits the upload size and temporary directory Nginx/Apache prohibits access to the upload directory; 4. The GD library resaves the pictures to clear potential malicious data.

There are three common methods for PHP comment code: 1. Use // or # to block one line of code, and it is recommended to use //; 2. Use /.../ to wrap code blocks with multiple lines, which cannot be nested but can be crossed; 3. Combination skills comments such as using /if(){}/ to control logic blocks, or to improve efficiency with editor shortcut keys, you should pay attention to closing symbols and avoid nesting when using them.

AgeneratorinPHPisamemory-efficientwaytoiterateoverlargedatasetsbyyieldingvaluesoneatatimeinsteadofreturningthemallatonce.1.Generatorsusetheyieldkeywordtoproducevaluesondemand,reducingmemoryusage.2.Theyareusefulforhandlingbigloops,readinglargefiles,or

The key to writing PHP comments is to clarify the purpose and specifications. Comments should explain "why" rather than "what was done", avoiding redundancy or too simplicity. 1. Use a unified format, such as docblock (/*/) for class and method descriptions to improve readability and tool compatibility; 2. Emphasize the reasons behind the logic, such as why JS jumps need to be output manually; 3. Add an overview description before complex code, describe the process in steps, and help understand the overall idea; 4. Use TODO and FIXME rationally to mark to-do items and problems to facilitate subsequent tracking and collaboration. Good annotations can reduce communication costs and improve code maintenance efficiency.

TolearnPHPeffectively,startbysettingupalocalserverenvironmentusingtoolslikeXAMPPandacodeeditorlikeVSCode.1)InstallXAMPPforApache,MySQL,andPHP.2)Useacodeeditorforsyntaxsupport.3)TestyoursetupwithasimplePHPfile.Next,learnPHPbasicsincludingvariables,ech

In PHP, you can use square brackets or curly braces to obtain string specific index characters, but square brackets are recommended; the index starts from 0, and the access outside the range returns a null value and cannot be assigned a value; mb_substr is required to handle multi-byte characters. For example: $str="hello";echo$str[0]; output h; and Chinese characters such as mb_substr($str,1,1) need to obtain the correct result; in actual applications, the length of the string should be checked before looping, dynamic strings need to be verified for validity, and multilingual projects recommend using multi-byte security functions uniformly.

ToinstallPHPquickly,useXAMPPonWindowsorHomebrewonmacOS.1.OnWindows,downloadandinstallXAMPP,selectcomponents,startApache,andplacefilesinhtdocs.2.Alternatively,manuallyinstallPHPfromphp.netandsetupaserverlikeApache.3.OnmacOS,installHomebrew,thenrun'bre
